locked
What is alternate of AllowWriteStreamBuffering in HttpWebRequest as I am not able to find it in winrt api

    Question

  • Hello,

    I am having an old code which need to be used in win 8 app, but cannot find appropriate solution.

    Following is the code used for "GET" request, can anybody help to convert it in win 8 apps?

    webReq = (HttpWebRequest)WebRequest.Create(url);
    webReq.Timeout = 5000;
    webReq.Headers["Cookie"] = userSession.ToString();
    webReq.AllowWriteStreamBuffering = false;
    webReq.AllowAutoRedirect = true;

    Thanks in advance.




    • Edited by zee_patel Friday, May 02, 2014 7:03 AM
    Friday, May 02, 2014 7:02 AM

Answers

  • What does your Set-Cookie HTTP Header look like? When you set the AllowAutoRedirect, is there any HTTP redirection happening? The Set-Cookie HTTP Header has a domain as well as a path attribute, so if your Set-Cookie header contains a domain and/or path value for a.com, and you are redirecting to b.com, and the same holds true for path as well, then your second request will not carry the Cookie header.

    You can take a look at the Set-Cookie HTTP header to understand what is happening. If you have a server that I can hit, then you can share the URL here.


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    • Marked as answer by zee_patel Thursday, May 08, 2014 5:32 AM
    Monday, May 05, 2014 4:38 PM
    Moderator

All replies

  • You should start using the Windows.Web.Http.HttpClient instead of using the old System.Net.HttpWebRequest class for Windows Store apps. That property is not accessible through the HttpWebRequest in Windows Store apps (only available for desktop).

    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Friday, May 02, 2014 5:50 PM
    Moderator
  • Hi Prashant,

    Thanks for the reply. I tried HttpClient but the problem is that cookie is not setting for the second request when allowautoredirect is set to true. I can't figure out what is going wrong.

    Here is the code.

     var handler = new HttpClientHandler();
     handler.Proxy = null;
     handler.UseCookies = true;
     handler.AllowAutoRedirect = true;
     HttpResponseMessage aResponse = null;
    
    var httprequestMessage = new HttpRequestMessage(HttpMethod.Get, new Uri(url));
     if (userSession != null && !String.IsNullOrEmpty(userSession.ToString()))
          handler.CookieContainer.SetCookies(new Uri(url), userSession.ToString());
     else
          handler.CookieContainer = new CookieContainer();
    
    
     using (HttpClient httpClient = new HttpClient(handler))
      {
           httpClient.BaseAddress = new Uri(url);
          httpClient.Timeout = TimeSpan.FromMilliseconds(5000);
                   
          aResponse = await httpClient.GetAsync(new Uri(url)).ConfigureAwait(false);
                        
       }
    

    Monday, May 05, 2014 7:22 AM
  • What does your Set-Cookie HTTP Header look like? When you set the AllowAutoRedirect, is there any HTTP redirection happening? The Set-Cookie HTTP Header has a domain as well as a path attribute, so if your Set-Cookie header contains a domain and/or path value for a.com, and you are redirecting to b.com, and the same holds true for path as well, then your second request will not carry the Cookie header.

    You can take a look at the Set-Cookie HTTP header to understand what is happening. If you have a server that I can hit, then you can share the URL here.


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    • Marked as answer by zee_patel Thursday, May 08, 2014 5:32 AM
    Monday, May 05, 2014 4:38 PM
    Moderator
  • Hi Prashant,

    Thanks for the reply, as you suggest now I am setting path of the cookie but there is a little problem with it.

    Here is the code:

    if (userSession != null && !String.IsNullOrEmpty(userSession.ToString()))
    {
     string cookieH = userSession.ToString() + @"; domain=mydomain; path=/";
    
      handler.CookieContainer.SetCookies(new Uri(url), cookie);
    }
    else
    handler.CookieContainer = new CookieContainer();
    
    
    

    And I am having tostring override method :

     public override string ToString()
     {
         return String.Format("dummy1={0}, dummy2={1}, dummy3={2}, dummy4={3}", this.dummy1, this.dummy2, this.dummy3, this.dummy4);
     }

    When the url is redirected instead of complete cookie only dummy4="value" is been sent.

    Note: Domain remains same for both request.

     

    Tuesday, May 06, 2014 6:23 AM
  • It looks like you are using the System.Net.HttpClient/ HttpClientHandler class.

    You should really look into using the brand new Windows.Web.Http.HttpClient class (notice the different namespace) which is based on the WinINet stack and move away from the old System.Net namespace/classes for Windows Store apps.

    For your question though, the domain and path values are only set in the Set-Cookie response header from the server, and not in the client side HTTP Cookie header.

    For example:

    The server response will look like this:

    Set-Cookie: cookie1=oreo; Path=/; Domain=example.com

    and in response to this - if you have set the CookieContainer property on the HttpClientHandler, the subsequent request to example.com should have:

    Cookie: cookie1=oreo.

    The "Cookie" header itself will not carry the domain and path information as per RFC 6265: http://www.rfc-editor.org/rfc/rfc6265.txt The domain and path values that were defined for the Cookie header in RFC 2965 has been obsoleted by RFC 6265. Hope that helps.


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog


    Tuesday, May 06, 2014 5:47 PM
    Moderator
  •  Hi Prashant,

    Thanks for your reply. But as I am making app for windows 8. I can't use Windows.Web.Http.HttpClient.

    Anyway I found another solution for it and its works fine for me.

     string cookie1 = "Dummy1=" + value1 + @"; domain=mydomain; path=/";
       string cookie2 = "Dummy2=" + value2 + @"; domain=mydomain; path=/";
       string cookie3 = "Dummy3=" + value3 + @"; domain=mydomain; path=/";
       string cookie4 = "Dummy4=" + value4 + @"; domain=mydomain; path=/";
    
       handler.CookieContainer.SetCookies(new Uri(url), cookie1);
       handler.CookieContainer.SetCookies(new Uri(url), cookie2);
       handler.CookieContainer.SetCookies(new Uri(url), cookie3);
       handler.CookieContainer.SetCookies(new Uri(url), cookie4);

    Thursday, May 08, 2014 5:32 AM