我正在尝试使用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>();
原文地址:https://www.jb51.cc/csharp/98822.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。