Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - Receiving strange character from HttpWebResponse

I have this code to send a HTTP Request:

public string MakeRequest(string requestUrl, object data)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUrl);
    request.ContentType = "application/json";
    request.KeepAlive = false;
    request.Headers.Add("Authorization", "BEARER " + apiToken);
    System.Net.ServicePointManager.Expect100Continue = false;

    if (data != null)
    {
        request.Method = "POST";
        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            string json = new JavaScriptSerializer().Serialize(data);

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }
    }
    else
        request.Method = "GET";

    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
        if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Created)
            throw new Exception(String.Format("Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription));

        string Charset = response.CharacterSet;
        Encoding encoding = Encoding.GetEncoding(Charset);
        StreamReader reader = new StreamReader(response.GetResponseStream(), encoding);

        return reader.ReadToEnd();
    }
}

It works well for most calls but one POST where I receive this as response:

"�\b\0\0\0\0\0\0�V�M,.I-�/JI-R��V3<S����L�L,�L��jk[���&\0\0\0"

And when I see the call captured by Fiddler it says the routine received:

{
  "MasterOrder": {
    "OrderId": "65250824"
  }
}

So, what is happening exactly? How is that Fiddler sees one response and the applications sees another response?

like image 784
Joe Almore Avatar asked Nov 04 '16 01:11

Joe Almore


People also ask

What does C C++ mean?

C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for creating large-scale applications. C++ is a superset of the C language. A related programming language, Java, is based on C++ but optimized for the distribution of program objects in a network such as the Internet.

Which is easier C or C?

Answers: Actually, both are difficult and both are easy. C++ is built upon C and thus supports all features of C and also, it has object-oriented programming features. When it comes to learning, size-wise C is smaller with few concepts to learn while C++ is vast. Hence we can say C is easier than C++.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

What is %d in C programming?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.


1 Answers

Solved the issue by adding these lines to the request:

request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
like image 108
Joe Almore Avatar answered Oct 04 '22 08:10

Joe Almore