0

I have code that send HttpRequest and get json in string. The code work fine but it's contain diamond and unreadable string when server start sending tranfer-encoding chunked response I have tried to use buffer and read the text into stringBuilder but it doesn't work well. I read in some answer that HttpWebRequest is obsolete and I should use HttpClient. I have rewrite my code in HttpClient but problem remain the same. I open the url in browser and it's work fine, it's took seconds to load and I didn't see transfer-encoding Chunked.

Please check the code,

        HttpClientHandler handler = new HttpClientHandler();
        handler.CookieContainer = cookies;

        var client = new HttpClient(handler);

        var ans = client.GetAsync(strURL).Result;
        var resStream = ans.Content.ReadAsStringAsync().Result;

I have applied the 8192 byte buffer and read from SO, doesn't work with this code and tried many code from the SO in past few hours. Please check what is wrong in my code. can I set something in header to simple say no to chunked html or there is a way I can read the chunked html from the response.

enter image description here

Anirudha Gupta
  • 9,073
  • 9
  • 54
  • 79
  • I strongly suspect that this has nothing to do with the transfer encoding. Does this help: https://stackoverflow.com/a/28131841/14357 ? – spender Sep 07 '18 at 11:13
  • @spender please check the screenshot I added in my code, I got text like this, the same url pasted in browser work but in c# It's not readable. – Anirudha Gupta Sep 07 '18 at 11:44

1 Answers1

0

According to your code, the returned json is not in string format, it is byte array? It is related to server side, does the server encode the content before sending back? Sorry, I was trying to add comments, but my level is too low, I can only post answers, hope some of my ideas can help.

in my project, server side, we use the code below to send response

public System.Net.Http.HttpResponseMessage GetServiceStatus()
{
    ......
    string sJsonRet = JsonHelper.Serialize(rspMsg);
    System.Net.Http.HttpResponseMessage resp = new System.Net.Http.HttpResponseMessage(System.Net.HttpStatusCode.OK);
    resp.Content = new System.Net.Http.StringContent(sText,System.Text.Encoding.UTF8, "text/plain");
        return resp;        
}

Client side, you always get plain text string and we use System.Net.WebClient.

 internal void GetServiceStatusCompleted(object sender, UploadStringCompletedEventArgs e){
......
   string response = e.Result;
   if ((response != null) && (response.Length > 0))
      status = (ServiceStatusMsg)JsonHelper.Deserialize(typeof(ServiceStatusMsg), response);

}

Victor Xie
  • 148
  • 1
  • 9