【发布时间】:2018-06-15 14:02:06
【问题描述】:
我正在尝试使用 HttpClient 将 XML 发送到私有 API。服务器说他们期望:带有 xml 在名为“request”的变量中的 HTML Post。
我的代码:
XDocument document = new XDocument();
//Here I create the XML file
HttpClient client = new HttpClient();
// URL is the url for the private server
client.BaseAddress = new Uri(URL);
string request = document.ToString();
var httpConten = new StringContent(request, Encoding.UTF8, "application/xml");
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
var result = client.PostAsync("", httpConten).Result;
结果正在返回:
{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Connection: keep-alive
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff
Referrer-Policy: same-origin
Strict-Transport-Security: max-age=15768000; includeSubDomains
Cache-Control: no-store
Date: Thu, 14 Jun 2018 23:16:27 GMT
Set-Cookie: SESSION=8dfda17c-a5c9-44c6-ae2b-d74847039f87; Path=/; Secure; HttpOnly
Server: Apache-Coyote/1.1
Content-Length: 469
}}
我是否正在使用名为 request 的 XML 变量发布 HTML 帖子?我联系了服务器,他们说他们没有收到我的请求。
谢谢
【问题讨论】:
-
仔细检查 URL。如果它是致命的,那么告诉服务器是否需要任何类型的身份验证 - 基本、令牌等?
-
他们在 XML 文件中进行身份验证。他们说问题是因为我没有在名为“request”的变量中提交 xml。我怎样才能确保我正在这样做?
标签: asp.net httpclient