【问题标题】:WebClient can't download stringWebClient 无法下载字符串
【发布时间】:2014-01-12 00:29:53
【问题描述】:

为什么这不起作用 -

“底层连接已关闭:连接意外关闭”

    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

【问题讨论】:

  • 你能使用像 Fiddler 这样的网络调试器来检查请求的情况吗?这可能会给出提示。

标签: c# webclient


【解决方案1】:

服务器似乎阻塞了,因为没有用户代理标头。这解决了它:

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;
    }
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-18
  • 1970-01-01
  • 1970-01-01
  • 2015-02-20
相关资源
最近更新 更多