【问题标题】:webclient The remote server returned an error: (500) Internal Server Errorwebclient 远程服务器返回错误:(500) Internal Server Error
【发布时间】:2013-02-17 09:23:10
【问题描述】:

我正在尝试使用 webclient 下载网页并收到 500 内部错误

public class AsyncWebClient
    {
        public string GetContent(string url)
        {
            return GetWebContent(url).Result.ToString();
        }
        private Task<string> GetWebContent(string url)
        {
            var wc = new WebClient();
            TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();

            wc.DownloadStringCompleted += (obj, args) =>
            {
                if (args.Cancelled == true)
                {
                    tcs.TrySetCanceled();
                    return;
                }

                if (!String.IsNullOrEmpty(args.Result))
                    tcs.TrySetResult(args.Result);
            };

            wc.DownloadStringAsync(new Uri(url));
            return tcs.Task;
        }
    }

然后调用:

var wc =new AsyncWebClient();
var html = wc.GetContent("http://truyen.vnsharing.net/");    >> always get the above error

如果我使用其他网站,那么它工作得很好。不知道这个网站有什么特别之处。

请帮忙!!

【问题讨论】:

    标签: c# httpwebrequest webclient


    【解决方案1】:

    服务器很可能期待一个正确的用户代理。

    将您的代码更新为以下内容:

    var wc = new WebClient();
    wc.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");
    TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
    

    【讨论】:

    • 如果可能,请不要冒充浏览器。
    • svick 在代码中不模拟浏览器的原因是什么?
    猜你喜欢
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 2020-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多