我想使用HttpClient设置Content-Type和Authorization Headers,并希望为正文(请求)加载xml文件并发送Post请求。
我搜索了合并标头和正文以获取Post请求。
使用HttpRequestMessage设置标题。
HttpClient clientTest = new HttpClient();
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post,url);
httpRequest.Content = new StringContent(string.Empty, Encoding.UTF8, "application/vnd.citrix.sessionparams+xml");
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("CitrixAuth", "H4sIAAAAAAAEAK1X2Y6jyBL9lZLnEbnZt1J");
设置xml正文-
XDocument xml = XDocument.Load("RequestSession.xml");
转换为Document.ToString()
var httpContent = new StringContent(xml.Document.ToString(), Encoding.UTF8, "application/vnd.citrix.sessionstate+xml");
请帮助我结合使用HttpClient的标头和正文进行Post Request。
答案 0 :(得分:0)
我不知道这是否是您正在调用的服务的有效请求,但是要设置标头和正文并发送请求,它看起来像这样:
HttpClient clientTest = new HttpClient();
HttpRequestMessage httpRequest = new HttpRequestMessage(HttpMethod.Post,url);
httpRequest.Content = new StringContent(xml.Document.ToString(), Encoding.UTF8, "application/vnd.citrix.sessionstate+xml");
httpRequest.Headers.Authorization = new AuthenticationHeaderValue("CitrixAuth", "H4sIAAAAAAAEAK1X2Y6jyBL9lZLnEbnZt1J");
var response = await clientTest.SendAsync(httpRequest);
如果您收到的400错误请求是因为发送的内容不适合该API,不是因为您发送的方式。