我正在尝试获取HttpResponseMessage的内容.它应该是:{"message":"Action '' does not exist!","success":false}但我不知道如何从HttpResponseMessage中获取它.
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await httpClient.GetAsync("http://****?action=");
txtBlock.Text = Convert.ToString(response); //wrong!
Run Code Online (Sandbox Code Playgroud)
在这种情况下,txtBlock将具有以下值:
StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
Vary: Accept-Encoding
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Date: Wed, 10 Apr 2013 20:46:37 GMT
Server: Apache/2.2.16
Server: (Debian)
X-Powered-By: PHP/5.3.3-7+squeeze14
Content-Length: 55
Content-Type: text/html
}
Run Code Online (Sandbox Code Playgroud) 我使用HttpWebRequest使用以下代码读取网页:
var pageurl = new Uri(url, UriKind.Absolute);
var request = (HttpWebRequest)WebRequest.Create(pageurl);
request.Method = "GET";
request.AutomaticDecompression = DecompressionMethods.GZip;
request.KeepAlive = false;
request.ConnectionGroupName = Guid.NewGuid().ToString();
request.ServicePoint.Expect100Continue = false;
request.Pipelined = false;
request.MaximumResponseHeadersLength = 4;
if (ignoreCertificateErrors)
{
ServicePointManager.ServerCertificateValidationCallback += AcceptAllCertificatePolicy;
}
var response = (HttpWebResponse)request.GetResponse();
if (response != null)
{
using (var reader = new StreamReader(response.GetResponseStream()))
{
return reader.ReadToEnd();
}
}
Run Code Online (Sandbox Code Playgroud)
当传递的语言是英语时,这种方法非常有效,但当其他语言如西班牙语时,我会在返回的内容中获得大量的 .
代码是否有问题,或者是否存在我缺少的编码方式?