我从以下帖子开始创建我的方法: Stack Overflow Post
我创建了以下方法:
public HttpResponseMessage GetStuffFromRest(MyRequest myData)
{
var baseAddress = new Uri(_baseUri);
var requestXML = CreateBody(myData);
var response = new HttpResponseMessage();
var cookieContainer = new CookieContainer();
using (var handler = new HttpClientHandler() { CookieContainer = cookieContainer })
using (HttpClient httpClient = new HttpClient(handler) { BaseAddress = baseAddress })
{
#region Trying Cookie Container
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", "myUserName");
var c = new Cookie("Cookie", HttpContext.Current.Server.UrlEncode(myData.Cookie));
cookieContainer.Add(baseAddress, c);
response = httpClient.PostAsync(_myMethodUri, new StringContent(requestXML, Encoding.UTF8, "application/xml")).Result;
#endregion
}
return response;
}
myData.Cookie是从我调用并从中获取cookie的其他休息服务填充的。
当我打电话给另一个网络服务时,我收到了401状态代码。
我认为这与我如何将cookie传递给其他服务有关。
如何验证我的标头包含的内容? (特别是饼干)
更新: 重述了这个问题。