locked
HttpStreamContent won't save stream. RRS feed

  • Question

  • I need to set up a HttpStreamContent so that I can attach it to the new Windows.Web.Http.HttpClient but whenever I try to initialize it with the stream of information it still says that the content length is null. At the moment this is what I have.

    Windows.Storage.Streams.InMemoryRandomAccessStream contentStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
    Windows.Storage.Streams.DataWriter dw = new Windows.Storage.Streams.DataWriter(contentStream);
    dw.WriteBytes(System.Text.Encoding.UTF8.GetBytes(body));
    dw.StoreAsync().AsTask();
    content = new Windows.Web.Http.HttpStreamContent(contentStream);
    content.Headers.Add("Content-Type", "application/json; charset=utf-8");

    When I step through it, it shows that it has a length of 40 bytes when I get to the content initialization. However, after I finish the initialization the content in the HttpStreamContent is still null. 

    Any help would be appreciated.

    Monday, October 14, 2013 5:17 PM

Answers

  • Try the below code, the important lines are to FlushAsync(), DetachStream() and contentStream.Seek(0).

                    const int contentLength = 1000;                
                    // Generate sample data.
                    byte[] subData = new byte[contentLength];
                    for (int i = 0; i < subData.Length; i++)
                    {
                        subData[i] = 65; // Test data - character 'A'
                    }
    
                    Windows.Storage.Streams.InMemoryRandomAccessStream contentStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
                    Windows.Storage.Streams.DataWriter dw = new Windows.Storage.Streams.DataWriter(contentStream);
                    dw.WriteBytes(subData);
                    await dw.StoreAsync();
                    await dw.FlushAsync();
                    dw.DetachStream();
    
                    contentStream.Seek(0);
                    HttpStreamContent streamContent = new Windows.Web.Http.HttpStreamContent(contentStream);
                    streamContent.Headers.Add("Content-Type", "application/json; charset=utf-8");


    Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog, http://aka.ms/t4vuvz

    • Marked as answer by jshore Monday, October 14, 2013 7:18 PM
    Monday, October 14, 2013 6:49 PM
    Moderator
  • Thanks for the response! 

    This worked. Thanks so much. As a further note I also found right before I saw your response that this works as well:

    Windows.Storage.Streams.InMemoryRandomAccessStream contentStream = new Windows.Storage.Streams.InMemoryRandomAccessStream(); Windows.Storage.Streams.DataWriter dw = new Windows.Storage.Streams.DataWriter(contentStream); dw.WriteBytes(System.Text.Encoding.UTF8.GetBytes(body)); dw.StoreAsync().AsTask();

    content = new Windows.Web.Http.HttpStreamContent(contentStream.GetInputStreamAt(0)); content.Headers.Add("Content-Type", "application/json; charset=utf-8");

    I added the .GetInputStreamAt(0) for the initialization and it worked as well. 

    Thanks again for the response.

    • Marked as answer by jshore Monday, October 14, 2013 7:18 PM
    Monday, October 14, 2013 7:12 PM

All replies

  • Try the below code, the important lines are to FlushAsync(), DetachStream() and contentStream.Seek(0).

                    const int contentLength = 1000;                
                    // Generate sample data.
                    byte[] subData = new byte[contentLength];
                    for (int i = 0; i < subData.Length; i++)
                    {
                        subData[i] = 65; // Test data - character 'A'
                    }
    
                    Windows.Storage.Streams.InMemoryRandomAccessStream contentStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
                    Windows.Storage.Streams.DataWriter dw = new Windows.Storage.Streams.DataWriter(contentStream);
                    dw.WriteBytes(subData);
                    await dw.StoreAsync();
                    await dw.FlushAsync();
                    dw.DetachStream();
    
                    contentStream.Seek(0);
                    HttpStreamContent streamContent = new Windows.Web.Http.HttpStreamContent(contentStream);
                    streamContent.Headers.Add("Content-Type", "application/json; charset=utf-8");


    Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog, http://aka.ms/t4vuvz

    • Marked as answer by jshore Monday, October 14, 2013 7:18 PM
    Monday, October 14, 2013 6:49 PM
    Moderator
  • Thanks for the response! 

    This worked. Thanks so much. As a further note I also found right before I saw your response that this works as well:

    Windows.Storage.Streams.InMemoryRandomAccessStream contentStream = new Windows.Storage.Streams.InMemoryRandomAccessStream(); Windows.Storage.Streams.DataWriter dw = new Windows.Storage.Streams.DataWriter(contentStream); dw.WriteBytes(System.Text.Encoding.UTF8.GetBytes(body)); dw.StoreAsync().AsTask();

    content = new Windows.Web.Http.HttpStreamContent(contentStream.GetInputStreamAt(0)); content.Headers.Add("Content-Type", "application/json; charset=utf-8");

    I added the .GetInputStreamAt(0) for the initialization and it worked as well. 

    Thanks again for the response.

    • Marked as answer by jshore Monday, October 14, 2013 7:18 PM
    Monday, October 14, 2013 7:12 PM
  • Glad to know that you have a working scenario. the difference between the contentStream.Seek(0) and contentStream.GetInputStreamAt(0) is: using the first approach actually sends the Content-Length HTTP header followed by the entity body, whereas the second approach uses the Transfer-Encoding: chunked HTTP header, followed by the HTTP chunks. This is helpful in scenarios where the target server only accepts a "specific" format of the HTTP request.


    Windows Store Developer Solutions #WSDevSol || Want more solutions? See our blog, http://aka.ms/t4vuvz

    Monday, October 14, 2013 7:40 PM
    Moderator