Home > Software design >  C# WebException Response Parsing Issues
C# WebException Response Parsing Issues

Time:04-05

I am making an API call to a third party API, the code snippet was made to make a request and extract the response body from the response into a string in .Net 3.1. I can parse successful responses normally and successfully. We were able to parse WebException responses up to yesterday, where now they just return garbled string down below.

public dynamic RequestSender(HttpWebRequest request, dynamic vm)
        {
            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            Byte[] byteArray = encoding.GetBytes(JsonConvert.SerializeObject(vm));
            request.ContentLength = byteArray.Length;
            request.ContentType = @"application/json; ";
            try
            {
                System.IO.Stream os = request.GetRequestStream();
                os.Write(byteArray, 0, byteArray.Length); //Push it out there
                os.Close();
                System.Net.WebResponse resp = request.GetResponse();
                System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
                var responseString = sr.ReadToEnd().Trim();
                return responseString;
            }
            catch (WebException webErr)
            {
                return webErr;
                System.IO.StreamReader sr = new System.IO.StreamReader(webErr.Response.GetResponseStream());
                var responseString = sr.ReadToEnd().Trim();
                return responseString;
            }
            catch (Exception err)
            {
                return err;
            }
        }

The expected response should be:

{
    "response": {
        "errors": [
            {
                "code": "111285",
                "message": "The postal code F3FKWFIOJEF is invalid for CA United States."
            }
        ]
    }
}

However we are getting this as a response:

"\u001f�\b\0\0\0\0\0\0\u0003\u0014�A\n�0\u0010\u0005Ы\f�\u00161�������E�|�@L�L\u0012)�w�n\u001f��\n[r2��\u0019�Y��G�)���9w:_��o��ן�3��K��W�\"J����\"9�\u0018�䛗��\b�J���A\U00069c02@\u0006m2���s�~\0\0\0��\u0003\0\u0010A�(\u007f\0\0\0"

What we've tried:

We've tried altering the request, altering the encoding that streamreader uses to parse the response body, alongside making sure that the response body has length, which it does.

Not sure what it could be as the third party API is unusual in its implementation in that it attaches a response body into a 400 Bad Request error.

CodePudding user response:

After a lot of searching and digging I was able to determine that previously when creating the HttpWebRequest somewhere in the background it knew to automatically decompress Gzip for the responses. However, after some indeterminate point in time, that instruction was left out which resulted in parsing errors. Turns out it wasn't the result of incorrect codecs, or casting types or response calls, but that "Deflate Gzip" wasn't part of the request anymore afterwards.

Our fix was solved by making sure to add:

request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

I also threw in the bottom chunk of code down there for good measure.

request.KeepAlive = true;

Thanks to this post for saving hours of frustation: HTTP call in C# is returning garbage data in the response

  • Related