2

I have the following code

CookieContainer container = new CookieContainer();
            HttpCookieCollection oCookies = HttpContext.Current.Request.Cookies;
            for (int j = 0; j < oCookies.Count; j++)
            {

                HttpCookie oCookie = oCookies.Get(j);

                    Cookie oC = new Cookie();

                    // Convert between the System.Net.Cookie to a System.Web.HttpCookie...
                    oC.Domain = HttpContext.Current.Request.Url.Host;
                    oC.Expires = oCookie.Expires;
                    oC.Name = oCookie.Name;
                    oC.Path = oCookie.Path;
                    oC.Secure = oCookie.Secure;
                    oC.Value = oCookie.Value;

                    container.Add(oC);

            }

             HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/test.ashx");
            request.ServicePoint.ConnectionLimit = 100;
            request.Timeout = 20000;
            //request.Credentials = CredentialCache.DefaultCredentials;
            request.ServicePoint.Expect100Continue = false;
            request.CookieContainer = container;
            request.Method = "POST";
            string formContent = "requestName=update&objectId=1&parentId=1";
            byte[] byteArray = Encoding.UTF8.GetBytes(formContent);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            using (Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
                dataStream.Flush();
                dataStream.Close();
            }
            try
            {
                var response = request.GetResponse();
                using (var stream = response.GetResponseStream())
                {
                    if (stream == null)
                    {
                        throw new Exception("no response");
                    }
                    using (var sr = new StreamReader(stream))
                    {
                        var content = sr.ReadToEnd();
                    }
                }
            }
            catch (WebException wex)
            {
                var pageContent = new StreamReader(wex.Response.GetResponseStream()).ReadToEnd();
                throw new Exception(pageContent);
            }

        }

I am attempting to POST the data to a page on behalf of the user, this is a process that happens programatically and the site uses forms authentication, so the code copies the current cookies, and adds the container to the HttpWebRequest. When the code runs it gets to the line where it calls request.GetResponse() but at this point the code stops, and eventually times out. However, i have a break point on the PageLoad of the page it is calling, once the timeout has occurred the code hits the breakpoint at the start of this page with the correct information from the POST and the session cookies as would be expected. Does anybody know why there is a call that leads to a timeout first or what is happening at this point?

Ross
  • 165
  • 2
  • 12
  • Perhaps a server issue: Under HTTP 1.1 (which is what the WebRequest is using), the data does not have to be contained in the same HTTP message as the POST command. The server must recognize that and send a "100 Continue" status response. Does your server do that ? if not, it's the problem. – Graffito Jul 02 '15 at 14:32
  • I've already set the Expect100Continue to false in the HttpWebRequest, so that would avoid that problem wouldn't it? – Ross Jul 02 '15 at 14:37
  • I have the same problema. First of all `oC.Domain = HttpContext.Current.Request.Url.Host;` is wrong. It must be the target domain of the webrequest.create. I think the problem is to use localhost but i can't find solution (my site won't use a domain) – Emanuele Apr 03 '17 at 14:53

1 Answers1

1

This instruction

request.ServicePoint.Expect100Continue = false;

does not operate because, even if you try to remove the Expect header from the HttpRequestHeaders collection, the header will be added by an internal "MakeRequestCommand()" executed when the request is actually made.

To address this problem, You can turn off the Expect100Continue behavior using the System.Net.ServicePointManager.Expect100Continue property.

More explanations here

Graffito
  • 1,658
  • 1
  • 11
  • 10
  • 1
    I tried adding ServicePointManager.Expect100Continue = false in both the code and as a web.config setting but I am still seeing this timeout occuring – Ross Jul 02 '15 at 15:23