Dilara Albayrak
Dilara Albayrak

Reputation: 444

404 error after adding Content-Encoding as request header

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);
}

Upvotes: 0

Views: 2145

Answers (2)

AdrianHHH
AdrianHHH

Reputation: 14066

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.

Upvotes: 1

Trupti Ranpise
Trupti Ranpise

Reputation: 31

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.

Upvotes: 1

Related Questions