简体   繁体   中英

Why do i have this error? Exception thrown: 'System.Net.WebException' in System.dll C#

I want to do a search in google using a free proxy server. Here is my code:

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();

What can i do in order to fix this problem? I use this list of proxy servers: www.gatherproxy.com/proxylist/anonymity/?t=Transparent

在此处输入图片说明

It's a problem with proxy server.
It's returning 503 error code or just not available.
When I'm removing request.Proxy= new WebProxy("http://209.150.146.27:8080", true); everything is working.
When I use another public proxy server everything is working too.
Example of refactored code with new proxy server and 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!");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM