locked
WinRT: Debugging GetRequestStreamAsync() RRS feed

  • Question

  • The code below just dies when trying to execute GetRequestStreamAsync(). I'm guessing there's some property in httpWebRequest that's null but needs to be populated. Any pointers on obvious things to look for, or how to get this to at least throw an exception?

    Thanks...

    HttpWebRequest httpWebRequest = null;
    WebResponse webResponse = null;
    
    httpWebRequest = (HttpWebRequest)WebRequest.Create(Url);
    
    try
    {
        byte[] bytes = Serialize(request);
    
        // Set HttpWebRequest Properties
        httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
        httpWebRequest.Method = "POST";
        httpWebRequest.ContentType = "text/xml; encoding='utf-8'";
        httpWebRequest.ContinueTimeout = 10;
    
    
        using (Stream requestStream = await httpWebRequest.GetRequestStreamAsync())
        {
            requestStream.Write(bytes, 0, bytes.Length);
            await requestStream.FlushAsync();
        }
    
        // Sends the HttpWebREquest, and waits for a response.
        webResponse = await httpWebRequest.GetResponseAsync();


    Randy

    Tuesday, November 12, 2013 8:12 PM

Answers

  • Can you elaborate on what you mean by the code dies? Does the code just hang or does it crash? If it throws an exception, can you catch the exception and report it here? The code above looks fine, except for the Continue timeout which looks very low, you have set it to 10 milliseconds. What is the target server you are trying to reach? Maybe try collecting a network trace - using Network Monitor to understand what exactly is going on over the wire and check if the destination server is even reachable. How long does the code just sit there?

    Secondly, is there any reason you are using the System.Net.HttpWebRequest class? Windows 8.1 has a new Windows.Web.Http.HttpClient class which you should be using for multiple reasons. http://channel9.msdn.com/Events/Build/2013/4-092

    Thanks,

    Prashant.


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


    Tuesday, November 12, 2013 9:32 PM
    Moderator

All replies

  • Can you elaborate on what you mean by the code dies? Does the code just hang or does it crash? If it throws an exception, can you catch the exception and report it here? The code above looks fine, except for the Continue timeout which looks very low, you have set it to 10 milliseconds. What is the target server you are trying to reach? Maybe try collecting a network trace - using Network Monitor to understand what exactly is going on over the wire and check if the destination server is even reachable. How long does the code just sit there?

    Secondly, is there any reason you are using the System.Net.HttpWebRequest class? Windows 8.1 has a new Windows.Web.Http.HttpClient class which you should be using for multiple reasons. http://channel9.msdn.com/Events/Build/2013/4-092

    Thanks,

    Prashant.


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


    Tuesday, November 12, 2013 9:32 PM
    Moderator
  • The video really helped. It allowed me to rewrite the message above so that at least I get an error message back from the server. It also helped me justify moving to Windows 8.1. Thanks for the good pointer.

    Randy

    Wednesday, November 13, 2013 10:35 PM