c# - HttpWebRequest 无法通过代理连接?

标签 c# .net proxy httpwebrequest

根据我读过的所有内容,我试图实现一些应该很简单的东西,但对我来说不起作用:通过代理发送任何请求。

请看下面的代码;只要注释掉这两行就可以了。一旦我将它们放回并尝试使用任何代理,请求就会不断超时,抛出“无法连接到远程服务器”WebException,内部异常消息“连接尝试失败,因为连接方在一段时间后没有正确响应”时间,或建立的连接失败,因为连接的主机无法响应 xxx.xxx.xxx.xxx:zzzz"。

http://www.ip-adress.com/Proxy_Checker/用于获取测试代理列表。

var request = (HttpWebRequest) WebRequest.Create("http://google.com/");
//var myproxy = new WebProxy("http://xxx.xxx.xxx.xxx:zzzz", false);
//request.Proxy = myproxy;
request.Method = "GET";
var response = (HttpWebResponse) request.GetResponse();

我显然遗漏了一些东西,我发现的所有类似问题要么有更复杂的问题,要么没有得到解答。

谢谢。

最佳答案

Uri address = new Uri("http://google.com/");               
// Create the web request 
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

// Set type to POST
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

request.Proxy = new WebProxy("ProxyIP", "Port");
request.Proxy.Credentials = new NetworkCredential("ProxyUsername", "ProxyPassword");

// Write data  
using (Stream postStream = request.GetRequestStream())
{
    postStream.Write(byteData, 0, byteData.Length);
}

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
    StreamReader streamReader = new StreamReader(response.GetResponseStream());
    string strReaderXML = streamReader.ReadToEnd();
}

关于c# - HttpWebRequest 无法通过代理连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20336826/

相关文章:

php - 如何在 PHP 中将 fsockopen(或兼容)与 SOCKS 代理一起使用?

c# - 不允许在查询中显式构造实体类型 'Artist'

c# - 用于更改 Internet Explorer 设置的 API(特别是 "Allow mixed content")

c# - 错误 : 22001: value too long for type character varying(255)

gwt - 如何通过 Google Web Toolkit 使用代理

proxy - 在代理后面拉取 docker 镜像

c# - WCF 单一联系点

.net - 在 WinDbg 中调试 devenv.exe 内存转储

c# - 有什么方法可以对 List<T> 的子类进行 JSON.NET 序列化,该子类也具有额外的属性?

.net - 为什么 ASP.NET Core Web 应用程序中有 Main() 方法(入口方法)?这种方法背后的原因是什么?