1

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) 
        {
            //convert only from this line
            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 this line
        {
                return DeserializeData(reader);
        }

Question 1: How can I achieve this using system.net.http namespace?

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.

Reference Link: https://learn.microsoft.com/en-us/dotnet/api/system.net.http?view=netframework-4.7.2 https://learn.microsoft.com/en-us/uwp/api/windows.web.http

Thanks

  • what have you had problems with? – jazb Dec 10 '18 at 02:39
  • it is written for windows.web.http library, I want to convert it to system.net.http library, which is different, that code wont work on system.net.http, Iam using system.net.http because Iam making a project in azure function, which is not compatible with windows.web.http. –  Dec 10 '18 at 03:05
  • 1
    show us your attempt at doing this task, then if you run into a problem we can perhaps assist at that point. or would you rather we just wrote the code for you? – jazb Dec 10 '18 at 03:08
  • sure I will edit the code base on I tried, regarding the reading as Stream, Iam not able to figure out the outcome of my change , cause of my issue from adding header, just a min Thank You –  Dec 10 '18 at 03:22
  • Can you check this answer: https://stackoverflow.com/a/12374486/7075463 – Tarik Tutuncu Dec 10 '18 at 05:55
  • Hi I tried to just use request.Headers.Add("SESSION", SessionId); on my current codebase(the one using windows.web.http) and works perfefctly. btw here is how I call it: var response = await _client.SendAsync(request, source.Token) .ConfigureAwait(false); –  Dec 10 '18 at 06:03
  • It is a bit unclear: are you trying to add header to response that you are going to deliver to a client? Or to response that you have got from a server? – Felix Dec 10 '18 at 20:26
  • @Felix , CreateHttpRequestMessage is my base class , this will create the base address, and then as you can see , I added a header on my httprequestmessage using headers.add , then Iam calling the created httprequest message using .SendAsync. –  Dec 11 '18 at 01:00
  • I separated my questitons , here is the link from my other question from header: https://stackoverflow.com/questions/53717977/how-to-properly-add-httprequestheaders-using-system-net-http-library Thanks –  Dec 11 '18 at 05:40

1 Answers1

0
using (var input =
                await response.Content.ReadAsStreamAsync()
                                      .ConfigureAwait(false))
            using (StreamReader sr = new StreamReader(input))
            using (JsonReader reader = new JsonTextReader(sr))
            {
                return DeserializeData(reader);
            }