2

I'm making a post request with HttpRequestMessage/HttpResponseMessage. Everything is okay, I'm getting the response into a string whose length is correct as expected, but if I want to display the raw string, it just returns one question mark character (?) and nothing else, as if the output is just one/two characters of length.

I've tried to decode the string to UTF8, still the same.

Here is my code:

var targetUrl = new Uri("https://www.targetUrl.com/");
var postData = GetPostData;
var request = httpClientHelp.GetRequest(HttpMethod.Post, targetUrl, postData);
var response = Await httpRequestProcess.SendAsync(request);
var data = Await response.Content.ReadAsStringAsync();
Debug.WriteLine(data.Length); // Correct, i.e. 5686
Debug.WriteLine(data); // Just displays "?"

//. Here is the SendAsync function from the httpRequestProcess class:

public async Task<HttpResponseMessage> SendAsync(HttpRequestMessage requestMessage)
{
    var response = await Client.SendAsync(requestMessage);
    return response;
}

Here is the raw string result

Here is the difference between the string content and its length

If I copy that question mark, the output is a multiple set of unknown characters, example: � �

Dezv
  • 156
  • 1
  • 8
  • 2
    Try with `Convert.FromBase64String(data);` – Anant Dabhi Aug 07 '19 at 11:50
  • It seems like youre getting the bytes length not the string characters length. Or is such a long string? – Mr.Deer Aug 07 '19 at 11:50
  • 1
    It doesn't work, Anant. It messes up with the string and throws an exception. – Dezv Aug 07 '19 at 11:52
  • 1
    It's a really long string. The length of the string matches with the response I anticipate. – Dezv Aug 07 '19 at 11:53
  • 1
    I think it's exactly the result Fiddler displays before decoding the response: http://prntscr.com/opizvi/direct But how do I decode it the way Fiddler does? What type of encoding is this? – Dezv Aug 07 '19 at 11:54
  • Try data.Result. – Anil Goel Aug 07 '19 at 11:55
  • 1
    Basically I captured the traffic and tried to mime the request exactly the way fiddler does, the result is the same, but Fiddle manages to decode the output, meanwhile my string is purely unknown characters. But I'm confused of why calling the .length returns the correct unencoded length. – Dezv Aug 07 '19 at 11:57
  • 1
    @AnilGoel It's literally the same thing, haha. It doesn't work, unfortunately. – Dezv Aug 07 '19 at 11:58
  • @Ebi-ツ Do you have access to the data you are sending. Can you post it? – Anil Goel Aug 07 '19 at 12:02
  • It seems like fiddler is not decoding it properly also, isnt it? – Mr.Deer Aug 07 '19 at 12:03
  • 1
    @JesúsNarváezTamés This is the output by default in Fiddler, I'd have to click the "Response body is encoded. Click to decode" button, and then fiddler will decode it. – Dezv Aug 07 '19 at 12:04
  • I have found this somewhere to get the encoding of the charset, maybe helps you ´var encoding = Encoding.GetEncoding(response.CharacterSet);´ – Mr.Deer Aug 07 '19 at 12:05
  • 1
    @AnilGoel The data is mainly formed out of personal info, I cannot share it, unfortunately. – Dezv Aug 07 '19 at 12:05
  • Which .Net version are you using ? – Mr.Deer Aug 07 '19 at 12:06
  • @Ebiツ Is it possible that data being send is in byte array. Try reading it as byte array and then convert it to string. – Anil Goel Aug 07 '19 at 12:13
  • 1
    @JesúsNarváezTamés 4.7 – Dezv Aug 07 '19 at 12:16
  • 1
    @AnilGoel http://prntscr.com/opje77/direct – Dezv Aug 07 '19 at 12:17
  • 1
    can you check in fiddler if this data is compressed, by any compression algo for ex Gzip ? – Sachin Vishwakarma Aug 07 '19 at 12:20
  • 1
    @SachinVishwakarma It doesn't show any, it seems that the default compression is set to none. – Dezv Aug 07 '19 at 12:23
  • 2
    @SachinVishwakarma I solved the issue. Will update my thread right now. – Dezv Aug 07 '19 at 12:29

1 Answers1

3

Thanks to @SachinVishwakarma I was able to solve the issue. The default encoding on the header was "gzip, deflate, br" as shown in Fiddler. All I had to do was initialize the HttpClient with a HttpClientHandler whose AutomaticDecompression property was optimized for all the compression methods.

Example:

    var _httpHandler = new HttpClientHandler() { CookieContainer = new System.Net.CookieContainer(), AutomaticDecompression = System.Net.DecompressionMethods.GZip | System.Net.DecompressionMethods.Deflate | System.Net.DecompressionMethods.None };
    var _httpClient = new HttpClient(_httpHandler);

Thank you guys for helping me out!

Dezv
  • 156
  • 1
  • 8