为什么我有这个错误?抛出异常:System.dll C中的'System.Net.WebException'#

时间:2016-06-26 11:59:30

标签: c#

我想使用免费代理服务器在谷歌搜索。这是我的代码:

string SearchResults = "http://google.com/search?q=" + textBox1.Text.Trim();
HttpWebRequest request;
HttpWebResponse response;
request = (HttpWebRequest)WebRequest.Create(SearchResults);
request.Method = "GET";      
request.Proxy= new WebProxy("http://209.150.146.27:8080", true);
//here is the issue
response = (HttpWebResponse)request.GetResponse();

我该怎么做才能解决这个问题? 我使用这个代理服务器列表: www.gatherproxy.com/proxylist/anonymity/?t=Transparent

enter image description here

1 个答案:

答案 0 :(得分:1)

代理服务器存在问题 它返回503错误代码或只是不可用。
当我删除request.Proxy= new WebProxy("http://209.150.146.27:8080", true);时,一切正常。
当我使用另一个公共代理服务器时,一切都正常工作 使用新代理服务器和try-catch的重构代码示例:

var searchResults = "http://google.com/search?q=" + textBox1.Text.Trim();

HttpWebResponse response = null;
var request = (HttpWebRequest)WebRequest.Create(searchResults);
request.Method = "GET";
request.Proxy = new WebProxy("http://92.46.122.98:3128", true);

try
{
    response = (HttpWebResponse) request.GetResponse();
}
catch (Exception ex)
{
    MessageBox.Show("Proxy server is probably do not working. Error message: "+ex.Message);
}

if (response != null)
{
    MessageBox.Show("Job done!");
}