为什么这不起作用-
“基础连接已关闭:连接意外关闭”
static void Main()
{
using (var client = new WebClient())
{
try
{
Console.WriteLine(client.DownloadString("http://oz.by/books/more10176026.html"));
}
catch (Exception e)
{
throw;
}
}
}
通常的GET请求都可以。检查here
最佳答案
服务器似乎阻塞,因为没有用户代理标头。可以解决此问题:
using (var client = new WebClient())
{
try
{
//Add your user agent of choice. This is mine, just as an example.
client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.73.11 (KHTML, like Gecko) Version/7.0.1 Safari/537.73.11");
Console.WriteLine(client.DownloadString("http://oz.by/books/more10176026.html"));
}
catch (Exception e)
{
throw;
}
}
关于c# - WebClient无法下载字符串,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20749448/