我只是想ping谷歌或某个网站,看看我是否可以使用此代码。我是C#的新手,所以我不知道为什么这是错的。有什么我想念的吗?
System.Net.WebClient myWebClient = new System.Net.WebClient();
myWebClient.Proxy = new System.Net.WebProxy("--mywebproxy--");
myWebClient.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)";
myWebClient.Headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
myWebClient.Headers["Accept-Language"] = "en-us,en;q=0.5";
myWebClient.Headers["Accept-Encoding"] = " gzip,deflate";
myWebClient.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
myWebClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
myWebClient.DownloadData("http://www.google.com");
答案 0 :(得分:0)
我希望您使用的是实际的URI
等
System.Net.WebClient myWebClient = new System.Net.WebClient { Proxy = new System.Net.WebProxy("http://127.0.0.1") };
如果仍然无效,请安装IIS并重试。
你的整个代码将是
var myWebClient = new System.Net.WebClient { Proxy = new System.Net.WebProxy("http://127.0.0.1") };
myWebClient.Headers["User-Agent"] = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2.6) Gecko/20100625 Firefox/3.6.6 (.NET CLR 3.5.30729)";
myWebClient.Headers["Accept"] = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
myWebClient.Headers["Accept-Language"] = "en-us,en;q=0.5";
myWebClient.Headers["Accept-Encoding"] = " gzip,deflate";
myWebClient.Headers["Accept-Charset"] = "ISO-8859-1,utf-8;q=0.7,*;q=0.7";
myWebClient.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
var x = myWebClient.DownloadData("http://www.google.com");