通过代理的C#HttpWebRequest请求 [英] C# HttpWebRequest request through proxy

查看:103
本文介绍了通过代理的C#HttpWebRequest请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的程序通过代理工作,但它不想这样做(System.Net.WebException:操作已超时). 没有代理,一切都很好

I'm trying to make my program work through proxy but it doesn't want to (System.Net.WebException: The operation has timed out). Without proxy everything is fine

这是一个代码:

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

代理还活着,我已经通过IE测试过.我该怎么做才能解决它?

proxy is alive, i've tested it via IE. What should i do to fix it?

推荐答案

一些建议:

  1. 您是否使用IP地址作为代理?
  2. 您需要登录到该代理吗? proxy.Credentials =新的NetworkCredential(User,Password);
  3. 尝试较少的标题,以少的标题开始,如果可行,请一一添加
  1. do you use IP address for the proxy?
  2. do you need to log in to that proxy? proxy.Credentials = new NetworkCredential(User, Password);
  3. try less headers, start with few and if it works keep adding one by one

UPD: 对于主机-这是一个有效的网址吗?您输入了有效的端口号吗? 就像www.contoso.com:8080

UPD: for the host - is it a valid URL? Did you put a valid port number? like www.contoso.com:8080

这篇关于通过代理的C#HttpWebRequest请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆