I'm developing a twitter third party application. while looking for a way to send a web request, I found two classes: Windows.Web.Http.HttpClient
and System.Net.Http.HttpClient
.
Both classes do not seem to have much difference, but they get very different esults in the same request.
When I send requests with Windows.Web.Http.HttpClient
, it works well.
public async Task<string> Request(Method method, string url, string postData)
{
var http = new Windows.Web.Http.HttpClient();
Windows.Web.Http.HttpResponseMessage response;
if (method == Method.POST)
{
var httpContent = new Windows.Web.Http.HttpStringContent(postData, Windows.Storage.Streams.UnicodeEncoding.Utf8);
httpContent.Headers.ContentType = Windows.Web.Http.Headers.HttpMediaTypeHeaderValue.Parse("application/x-www-form-urlencoded");
response = await http.PostAsync(new Uri(url), httpContent);
}
else
{
response = await http.GetAsync(new Uri(url));
}
return await response.Content.ReadAsStringAsync();
}
But If I send requests with System.Net.Http.HttpClient
. I receive wrong response.
(But, when I access the request url with a web browser, it works well not like image below)
public async Task<string> Request(Method method, string url, string postData)
{
var http = new System.Net.Http.HttpClient();
System.Net.Http.HttpResponseMessage response;
if (method == Method.POST)
{
var httpContent = new System.Net.Http.StringContent(postData, Encoding.UTF8, "application/x-www-form-urlencoded");
response = await http.PostAsync(new Uri(url), httpContent);
}else
{
response = await http.GetAsync(new Uri(url));
}
return await response.Content.ReadAsStringAsync();
}
Why is this different? and how can I solve this problem?