locked
Cookies lost when send httpwebrequest RRS feed

  • Question

  • I want to use HttpWebRequest to send post http request to remote server .Everything works fine.But I found a problem when attached the cookies to HttpWebRequest .
    As the following the code : I has a CookieContainer ,and it has some cookies which set by the latest request,I trace the cookies in it before send the request, it is correct.
    But when I send the request,  I use Charles(like fiddler) to see the Http request detail information,there is no any cookies information .

          CookieContainer cookieContainer = new CookieContainer();
    
          HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create("http://www.example.com");
          myRequest.Method = "POST";
          myRequest.CookieContainer = cookieContainer;
    
          #region Trace the requst cookies
          List<Cookie> cookies = GetAllCookies(myRequest.CookieContainer);
          StringBuilder st = new StringBuilder();
          foreach (Cookie cook in cookies)
          {
            st.AppendLine(string.Format("{0} = {1}", cook.Name, cook.Value));
            st.AppendLine(string.Format("Domain: {0}", cook.Domain));
            st.AppendLine(string.Format("Path: {0}", cook.Path));
            st.AppendLine(string.Format("Port: {0}", cook.Port));
            st.AppendLine(string.Format("Secure: {0}", cook.Secure));
          }
          string temp = st.ToString();
          Debug.WriteLine(temp);
          #endregion
          HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();
    
    

    I am use .net framework 4.0 .

    Does someone encounter the same problem ?

    Thanks for any advice . ...


     

    Monday, November 1, 2010 7:23 AM

Answers

  • I can confirm the CookieContainer has bugs .

    Even I set the CookieContainer which has cookies to the HttpWebRequest, there are still no cookies sent to the server .

    But I manually add a same domain cookie to the request , it sent ok .

    Somebody help ....


    Beat Microsoft.
    • Marked as answer by ms44cn Wednesday, November 3, 2010 6:58 AM
    Wednesday, November 3, 2010 3:45 AM

All replies

  • Any chance the domain/path/port/secure set on the cookie don't match / are not compatible with with the URL? 

     

     

     

    Monday, November 1, 2010 11:12 PM
  • The domain and path are same.

    I have no idea why the cookie can't set to the request  .

     

     

     


    Beat Microsoft.
    Tuesday, November 2, 2010 2:15 AM
  • I can confirm the CookieContainer has bugs .

    Even I set the CookieContainer which has cookies to the HttpWebRequest, there are still no cookies sent to the server .

    But I manually add a same domain cookie to the request , it sent ok .

    Somebody help ....


    Beat Microsoft.
    • Marked as answer by ms44cn Wednesday, November 3, 2010 6:58 AM
    Wednesday, November 3, 2010 3:45 AM