none
How to read HttpResponseContent as Stream and add Header Cookie using system.net.http

    Question

  • Good Day,

    I have a piece of code to read "HttpResponseContent" using "windows.web.http":

    using System.IO;
    using Windows.Web.Http;
    using Newtonsoft.Json;


    var responseContent = JsonConvert.DeserializeObject<HttpResponseContent>(response.Content.ToString()); if (response.StatusCode == HttpStatusCode.Ok) {

    //from here
                    using (var input =
                        await response.Content.ReadAsInputStreamAsync()
                                              .AsTask()
                                              .ConfigureAwait(false))
                    using (var stream = input.AsStreamForRead())
                    using (StreamReader sr = new StreamReader(stream))
                    using (JsonReader reader = new JsonTextReader(sr)) //to here
                    {
                        return DeserializeTerminals(reader);
                    }

    may I know how can I achieve this using "system.net.http" namespace?

    Also may I know how can I add Header cookie using "system.net.http"? , here my code using windows.web.http:

    public override HttpRequestMessage CreateHttpRequestMessage() {
                var request = base.CreateHttpRequestMessage();
    //this line request.Headers.Cookie.Add(new HttpCookiePairHeaderValue("SESSION", SessionId)); return request; }

    Here is how I call it:

    var source = new CancellationTokenSource(timeout)
    var response = await _client.SendAsync(request, source.Token)
                        .ConfigureAwait(false);

    Note: I wrote those codes in uwp using windows.web.http, what I want is to convert that code using different package/namespace, which is system.net.http which I am using in Azure Functions.

    UPDATE: I tried adding Header using this:

    request.Headers.Add("SESSION", SessionId);

    but Iam getting unauthorized, I am not sure why. or maybe its because it is not set from the Cookie

    Thanks






    • Moved by CoolDadTx Wednesday, December 5, 2018 3:12 PM UWA related
    • Edited by NicoTing 13 hours 35 minutes ago
    Wednesday, December 5, 2018 1:08 AM

All replies

  • Hi,

    What the type of your project is? Is it an UWP application or traditional desktop application?

    In UWP, Windows.Web.Http Namespace is recommended for use and it provides a modern HTTP client API for UWP app.

    Not all APIs in System.Net.Http namespace could be used in UWP apps. If you want to use the APIs within System.Net.Http namespace in UWP, please refer to the following document for more detailed information:

    System.Net.Http namespaces for UWP apps.

    Regards, 

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 6, 2018 8:39 AM
    Moderator
  • @Roy

    Azure Function
    Friday, December 7, 2018 3:45 AM
  • Hi,

    Do you mean you are using a Azure Function in your UWP app? Is this Function must be called using System.net.http namespace?

    Best regards,

    Roy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, December 7, 2018 7:16 AM
    Moderator
  • Hi Roy,

    No, what I meant is I wrote those codes in "uwp" using "windows.web.http", what I want is to convert that code using different package/namespace, which is "system.net.http" which I am using in Azure Functions.

    Thanks
    • Edited by NicoTing Monday, December 10, 2018 12:43 AM
    Monday, December 10, 2018 12:38 AM