因此,我尝试使用WebClient#downloadString(url),但似乎无法正常工作,这是错误消息:
An exception of type 'System.Net.WebException' occurred in System.dll but was not handled in user code
Additional information: The underlying connection was closed: The connection was closed unexpectedly.
我不确定为什么会发生这种情况或如何解决它,如果有人可以帮助我解决这个问题,那将是很棒的。我当前的代码是这样的:
string webData = new System.Net.WebClient().DownloadString("http://api.predator.wtf/host2ip/?arguments=www.google.com");
该代码主要仅用于测试,根本不起作用。有人有想法么?我四处张望,找不到解决此错误的任何方法。
您必须设置用户代理标头。如果用户代理为空,某些服务器将拒绝连接:
using (var w = new WebClient())
{
w.Headers.Add("user-agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36");
var s = w.DownloadString("http://api.predator.wtf/host2ip/?arguments=www.google.com");
Console.WriteLine(s);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句