使用Web客户端通过C#控制台应用程序

时间:2016-05-19 09:44:25

标签: c# proxy console-application webclient webproxy

我已经编写了一个代码来搜索特定的URL并使用WebClient下载数据,并已将我从MyPrivateProxies.net购买的代理设置为此webclient,其中包含凭据和其他所需的参数,通过这些参数可以访问所提到的URL。

但我每次都在最后一行得到这个例外。

  

System.Net.WebException:无法连接到远程服务器   System.Net.Sockets.SocketException:连接尝试失败   因为关联方在一段时间后没有正确回应   时间或已建立的连接因连接的主机而失败   未能回复

下面的

是我的代码片段:

WebClient client = new WebClient();
WebProxy webProxy = new WebProxy("my_proxy_with_port");
NetworkCredential netcredit = new NetworkCredential(username, password, domain);
client.Credentials = netcredit;
webProxy.Credentials = netcredit;
webProxy.UseDefaultCredentials = false;
webProxy.BypassProxyOnLocal = false;
client.Proxy = webProxy;
client.Encoding = Encoding.UTF8;

String strURL = "url_to_be_hit";
String jsonString = client.DownloadString(strURL);

我需要在Windows 7系统上进行其他一些设置或app.config中的任何配置更改吗?

控制台应用程序是否还有其他配置可以使用代理?

2 个答案:

答案 0 :(得分:0)

我认为您只需要将代理凭据分配给WebProxy对象,而不是WebClient。

我不知道myprivateproxies.net,但您必须确保使用的是http代理,而不是袜子代理或其他东西。

答案 1 :(得分:0)

得到它解决了我的代码问题或URI点或我传递的任何字符串......我的端口未在本地系统上打开