0

I have to add Content-Encoding and Accept-Encoding as headers(with gzip compression) to HttpWebRequest object. Setting Accept-Encoding is done by adding this line: request.AutomaticDecompression = DecompressionMethods.GZip; and it is ok. However, after setting Content-Encoding(which i'm not sure whether it is done correctly) with this line request.Headers.Add(HttpRequestHeader.ContentEncoding, "gzip");, i get 404 error. Here is my request code:

XmlDocument RequestAndResponseHelper(string requestStr, string directory)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(directory);

    var data = Encoding.ASCII.GetBytes(requestStr);
    request.Method = "POST";
    request.ContentLength = data.Length;
    request.ContentType = "text/xml";

    request.Headers.Add("userName", UserName);  
    request.Headers.Add("password", Password); 

    request.AutomaticDecompression = DecompressionMethods.GZip; //this adds  'Accept-Encoding: gzip' as request header
    request.Headers.Add(HttpRequestHeader.ContentEncoding, "gzip"); //this adds 'Content-Encoding: gzip' as request header

    using (var stream = request.GetRequestStream())
    {
        stream.Write(data, 0, data.Length);
    }

    var response = (HttpWebResponse)request.GetResponse();
    string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();

    XmlDocument rs = new XmlDocument();
    rs.LoadXml(responseString);

    return rs;
}

If you can help me to handle with that error, i'd really appreciate.

Also, here is my compression code, maybe there is something with compression.

String Compress(String requestStr)
{
    byte[] buffer = Encoding.UTF8.GetBytes(requestStr);
    MemoryStream ms = new MemoryStream();
    using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true))
    { zip.Write(buffer, 0, buffer.Length); }
    ms.Position = 0;
    MemoryStream outStream = new MemoryStream();
    byte[] compressed = new byte[ms.Length];
    ms.Read(compressed, 0, compressed.Length);
    byte[] gzBuffer = new byte[compressed.Length + 4];
    System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length);
    System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4);
    return Convert.ToBase64String(gzBuffer);
}
Dilara Albayrak
  • 314
  • 1
  • 3
  • 23

2 Answers2

1

I would suggest check your request uri. You will get a 404 only if your request uri is wrong. Either the resource does not exist on the server or your server is down.

if you had given a wrong value elsewhere in the headers like the ContentEncoding, means the origin server is not supporting the encoding, you get different error codes..here a 415 (unsupported) but not 404.

  • Thanks for your answer. However, if my request uri was wrong wouldn't i get 404 error without that line `request.Headers.Add(HttpRequestHeader.ContentEncoding, "gzip");` – Dilara Albayrak Dec 09 '16 at 06:04
1

The wrong header field is being set to "gzip".

ContentEncoding relates to character types in the data before any gzipping etc is done. It has values denoting encodings such as "Ascii", "UTF7", "UTF32", etc. See here and here.

"Gzip" is a MIME type specified via a HttpRequestHeader.ContentType. See here.

AdrianHHH
  • 13,492
  • 16
  • 50
  • 87
  • Actually, based on some search, i deduce similar thought like that. However, my service provider exactly says that "The following line should be added to the HTTP header: Content-Encoding: gzip". Then i thought that there should be some way to pass this as header. – Dilara Albayrak Dec 09 '16 at 13:27
  • Then should i embed gzip statement in this line of my code: `request.ContentType = "text/xml";` ? – Dilara Albayrak Dec 09 '16 at 13:28