简体   繁体   English

C# HttpWebRequest 通过代理请求

[英]C# HttpWebRequest request through proxy

I'm trying to make my program work through proxy but it doesn't want to (System.Net.WebException: The operation has timed out).我试图让我的程序通过代理工作,但它不想(System.Net.WebException:操作已超时)。 Without proxy everything is fine没有代理一切都很好

Here is a code:这是一个代码:

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

at the same time this code works well同时这段代码运行良好

        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 is alive, i've tested it via IE.代理还活着,我已经通过 IE 对其进行了测试。 What should i do to fix it?我该怎么做才能解决它?

Try adding the following into either your web.config or app.config depending on application type:尝试将以下内容添加到您的web.configapp.config具体取决于应用程序类型:

<configuration>

    <system.net>
        <defaultProxy>
            <proxy
                usesystemdefaults="true"
                proxyaddress="http://154.46.33.157:8080"
                bypassonlocal="true" />
              <bypasslist
                <add address="[a-z]+\.contoso\.com" />
            </bypasslist>
        </defaultProxy>
    </system.net>          

     <!-- The rest of your config here ... -->

</configuration>

You can find more details and additional parameters such as user credentials etc here:http://msdn.microsoft.com/en-us/library/kd3cf2ex(v=vs.110).aspx您可以在此处找到更多详细信息和其他参数,例如用户凭据等:http ://msdn.microsoft.com/en-us/library/kd3cf2ex(v=vs.110).aspx

few suggestions:几点建议:

  1. do you use IP address for the proxy?你使用IP地址作为代理吗?
  2. do you need to log in to that proxy?你需要登录那个代理吗? proxy.Credentials = new NetworkCredential(User, Password); proxy.Credentials = new NetworkCredential(User, Password);
  3. try less headers, start with few and if it works keep adding one by one尝试更少的标题,从几个开始,如果有效,请继续一一添加

UPD: for the host - is it a valid URL? UPD:对于主机 - 它是一个有效的 URL 吗? Did you put a valid port number?您是否输入了有效的端口号? like www.contoso.com:8080像 www.contoso.com:8080

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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