0

I am trying to login to a site using HttpWebRequest Post. It works perfectly fine. But when i use a proxy to login it wont work. The connection Timeouts, and if i remove the part:

request.Host="abc.com";

It works again fine with proxy , but doing the above will forbid me from login as the site needs that info . How can i over come this any suggestions?

CODE"

                HttpWebRequest httpWReq =
               (HttpWebRequest)WebRequest.Create(url);

                ASCIIEncoding encoding = new ASCIIEncoding();
                string postData = post;
                byte[] data = encoding.GetBytes(postData);

                string proxy = "58.20.127.26:3128";
               /////byte[] data = GetBytes(postData);
                WebProxy myProxy = new WebProxy(proxy);
                httpWReq.Proxy = myProxy;

                httpWReq.Method = "POST";
                httpWReq.Accept = "text/html, application/xhtml+xml, */*";
                httpWReq.Referer = refferr;
                httpWReq.CookieContainer = yumCookies;
                httpWReq.ContentType = "application/x-www-form-urlencoded";
                httpWReq.UserAgent = "xxxxxxxxxxx";
                httpWReq.Host = "xxx.com";
                httpWReq.Headers.Add("Accept-Language: en-US");
                httpWReq.ContentLength = data.Length;
                httpWReq.KeepAlive = true;
                httpWReq.Headers.Add("Pragma: no-cache");
                httpWReq.AllowAutoRedirect = true;
                httpWReq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

                using (Stream stream = httpWReq.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

                HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse();
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    Stream responseStream = response.GetResponseStream();
                    StreamReader myStreamReader = new StreamReader(responseStream);
                    responseString = myStreamReader.ReadToEnd();

                    if (response.Cookies.Count > 0)
                    {
                        foreach (Cookie ck in response.Cookies)
                        {
                            yumCookies.Add(ck);

                        }
                    }
                }
                response.Close();

                response = null;
                response = null;
confusedMind
  • 2,573
  • 7
  • 33
  • 74

1 Answers1

0

Can you share a little bit more details, such as how you create the request, set the proxy, etc? Normally you don't have to explicitly set the host like that.

Here is a copy of the code I have that works perfectly fine with proxies:

                    var req = WebRequest.Create("http://google.com") as HttpWebRequest;
                    req.Proxy = new WebProxy("proxy-ip", 8080); //proxy ip/port
                    req.ContentType = "text/html";
                    req.Method = "GET";
Dan
  • 734
  • 1
  • 9
  • 23
  • Yes it does for me too and mine too if i remove the part Host. Can you add req.Host='google.com' and then check if it works with proxy. – confusedMind May 01 '14 at 13:38
  • Try either setting the host in the header, or try another proxy because it might not be correctly passing the header – Dan May 01 '14 at 13:57
  • it happens the same with every proxy i have tried many , if i send host in header it gives error like not the place for it to be as it can be created as above. – confusedMind May 01 '14 at 13:58
  • i have seen that , it is not good for me as i need to change ip regularly that is similar to putting ip in IE and that works too. – confusedMind May 01 '14 at 14:05