复制代码 代码如下:
static void BasicMethod()
{
string url = "http://www.baidu.com";
HttpClient client = new HttpClient();
string content = client.GetStringAsync(url).Result;
Console.WriteLine(content);
}
复制代码 代码如下:
{
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Referrer = new Uri("http://www.google.com/");
request.Headers.Add("UserAgent", "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; WOW64; Trident/5.0; SLCC2; .NET CLR 2.0.50727)");
Task<HttpResponseMessage> task = base.SendAsync(request, cancellationToken);
HttpResponseMessage response = task.Result;
MediaTypeHeaderValue contentType = response.Content.Headers.ContentType;
if (string.IsNullOrEmpty(contentType.CharSet))
{
contentType.CharSet = "GBK";
}
return task;
}
}
复制代码 代码如下:
HttpClient client = new HttpClient() { MaxResponseContentBufferSize = 1024 * 1024 };
复制代码 代码如下:
HttpResponseMessage response = task.Result;
MediaTypeHeaderValue contentType = response.Content.Headers.ContentType;
if(string.IsNullOrEmpty(contentType.CharSet))
{
contentType.CharSet = "GBK";
}
新闻热点
疑难解答
图片精选