gpt4 book ai didi

C# HttpWebRequest 通过代理请求

转载 作者:太空宇宙 更新时间:2023-11-03 10:47:47 25 4
gpt4 key购买 nike

我试图让我的程序通过代理工作,但它不想(System.Net.WebException:操作超时)。没有代理一切都很好

代码如下:

        string proxy = "154.46.33.157";
int port = 8080;
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = "email=" + email + "&pass=" + pass;
byte[] data = encoding.GetBytes(postData);
WebProxy myproxy = new WebProxy(proxy, port);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("SITE");
WebHeaderCollection myWebHeaderCollection = request.Headers;
request.CookieContainer = sCookie;
request.Method = "POST";
request.Proxy = myproxy;
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.ContentLength = data.Length;
request.Host = "HOST";
request.UserAgent = "[UA]";
request.Referer = "reffer";
request.KeepAlive = false;
request.Timeout = 20000;

Stream stream = request.GetRequestStream(); // TIMEOUT HERE
stream.Write(data, 0, data.Length);
stream.Close();
request.GetResponse()
.Close();

同时这段代码运行良好

        string proxy = "154.46.33.157";
int port = 8080;
WebProxy myproxy = new WebProxy(proxy, port);
WebRequest req = WebRequest.Create("SITE");
req.Timeout = 5000;
req.GetResponse();

proxy 是活的,我已经通过 IE 测试过了。我应该怎么做才能修复它?

最佳答案

一些建议:

  1. 您是否使用 IP 地址作为代理?
  2. 您需要登录该代理吗?proxy.Credentials = new NetworkCredential(User, Password);
  3. 尝试减少 header ,从几个开始,如果可行,继续一个一个地添加

更新:对于主机 - 它是一个有效的 URL 吗?您是否输入了有效的端口号?像 www.contoso.com:8080

关于C# HttpWebRequest 通过代理请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22783889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com