繁体   English   中英

webclient远程服务器返回错误:(500)内部服务器错误

[英]webclient The remote server returned an error: (500) Internal Server Error

我正在尝试使用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

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

请帮忙 !!

服务器很可能期望正确的用户代理。

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

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>();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM