所以,这是我使用的代码
string URL = "http://www.test.com/posts/.json";
var getInfo = (HttpWebRequest)HttpWebRequest.Create(URL);{
getInfo.Headers["Cookie"] = CookieHeader;
getInfo.ContentType = "application/x-www-form-urlencoded";
using (WebResponse postStream = await getInfo.GetResponseAsync())
{
StreamReader reader = new StreamReader(postStream.GetResponseStream());
string str = reader.ReadToEnd();
}
我想切换到我已经开始工作的httpclient,除了它不传递Cookie信息。我得到了信息,但只是匿名信息。不是我发送的用户信息。这是我目前拥有的。
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
client.BaseAddress = new Uri("http://www.test.com/");
client.DefaultRequestHeaders.Add("Cookie", CookieHeader);
HttpResponseMessage response = await client.GetAsync("http://www.test.com" + URL);
string str;
str = await response.Content.ReadAsStringAsync();