c# – webclient远程服务器返回错误:(500)内部服务器错误
发布时间:2020-12-15 18:32:22 所属栏目:百科 来源:网络整理
导读:我正在尝试使用webclient下载网页并获取500内部错误 public class AsyncWebClient { public string GetContent(string url) { return GetWebContent(url).Result.ToString(); } private Taskstring GetWebContent(string url) { var wc = new WebClient(); T
我正在尝试使用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>(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |