1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

HttpWebRequest with proxies - problems

Discussion in 'C, C++, C#' started by crepito, Sep 28, 2010.

  1. crepito

    crepito Junior Member

    Joined:
    Oct 5, 2008
    Messages:
    145
    Likes Received:
    28
    Location:
    Portugal
    Hey guys, i'm having a lot of exceptions when using proxies and i dunno why.

    Most of the exceptions are:

    "The remote server returned an error: (417) Expectation Failed."
    "The underlying connection was closed: An unexpected error occurred on a receive."

    I'm using something like this:

    Code:
    
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("xxx");
                    WebHeaderCollection myWebHeaderCollection = req.Headers;
    
                    req.Proxy = new WebProxy(sProxy);
                    req.Host = "xxx";
                    req.KeepAlive = true;
                    req.Referer = "xxx";
                    req.ContentLength = postData.Length;
                    // Set the Cache-Control header in the request.
                    myWebHeaderCollection.Set("Cache-Control", "max-age=0");
                    myWebHeaderCollection.Set("Origin", "xxx");
    
                    req.ContentType = "application/x-www-form-urlencoded";
                    req.Accept =
                        "image/jpeg, image/png, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
                    req.UserAgent =
        "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; InfoPath.2; .NET4.0C)";
    
                    //myWebHeaderCollection.Set("Accept-Language:", "pt-PT,pt;q=0.8,en-US;q=0.6,en;q=0.4");
                    //myWebHeaderCollection.Set("Accept-Charset:", "ISO-8859-1,utf-8;q=0.7,*;q=0.3");
                    myWebHeaderCollection.Set("Accept-Encoding", "gzip, deflate");
                    req.Method = "POST";
                    req.CookieContainer = cookies;
    
                   System.Net.ServicePointManager.Expect100Continue = false;
    
                    Stream requestStream = req.GetRequestStream();
                    requestStream.Write(postData, 0, postData.Length);
                    requestStream.Flush();
                    requestStream.Close();
    
                    HttpWebResponse wrResult = (HttpWebResponse)req.GetResponse();
    
    If i don't use proxies i have no problem.

    Anyone know what i'm doing wrong?
     
    Last edited: Sep 28, 2010
  2. crepito

    crepito Junior Member

    Joined:
    Oct 5, 2008
    Messages:
    145
    Likes Received:
    28
    Location:
    Portugal
    I managed to fix my problem on my own.
     
    Last edited: Sep 28, 2010
  3. sodevrom

    sodevrom Jr. VIP Jr. VIP

    Joined:
    Sep 8, 2010
    Messages:
    2,181
    Likes Received:
    734
    Gender:
    Male
    Occupation:
    IM & BlackHat
    Location:
    011001110110111101100100
    Home Page:
    Next time, say the solution. What use is it to the forum members if you say:
    "I managed to fix my problem on my own."
     
  4. Monrox

    Monrox Power Member

    Joined:
    Apr 9, 2010
    Messages:
    615
    Likes Received:
    579
    Most probably you have to use asynchronous internet access with proxies. I switched to that and my bad proxies ratio dropped at least 70%. It made threads handling a bigger headache though but it's just to be expected.
     
  5. atomic999

    atomic999 Newbie

    Joined:
    Apr 22, 2009
    Messages:
    7
    Likes Received:
    1
    You can avoid the problem by setting correct Expect100Continue
    Code:
    Uri uri=new Uri(xws.Url);
    ServicePoint servicePoint = ServicePointManager.FindServicePoint(uri);
    if (servicePoint.Expect100Continue == true)
    {
       servicePoint.Expect100Continue = false;
    }
    
    I had same problem and it works ;)