none
Content-Type is net being set in HTTP header. Server returns 400 Bad Request error. RRS feed

  • Question

  • Hi,

    I am trying to access an XML WebService. This service requires the content type of the request to be set to "text/xml". As you can see in the source code, I am setting the req.ContentType property to "text/xml".

    However, this content type seems not to be added to the HTTP headers. The server returns a 400 Bad Request error as can be seen in the log.

    I've attached a System.Net.trace log and it states:

    [Public Key]
      Algorithm: RSA
      Length: 2048
      Key Blob: 30 82 01 0a 02 82 01 01 00 bc 09 30 8a 1e 03 4d 7a ea 16 d3 a8 5e d8 5b 00 c4 8a c5 9f 26 bd 7d d6 cb 8b d0 db bd 93 2d 2b 3b 84 f6 20 79 83 34 67 51 37 21 ea 56 5e 18 d8 a3 db 72 43 0e 14 77 e2 64 cb 07 b6 2a 81 c7 f5 16 dd 19 c7 d9 68 0b 3a 81 5c f0 05 c9 ed 2b 37 00 31 41 37 8b 3a 73 4a 4d ab d7 d8 87 79 35 82 01 97 e3 3c be bb 84 e5 94 bb 87 52 e3 9f b5 fb 3e 33 38 c3 eb 73 42 ee ba 1e c5 4a 33 18 a1 0d 8a d2 10 a8 c5 3....
    System.Net Information: 0 : [26780] SecureChannel#31884011 - Remote certificate was verified as valid by the user.
    System.Net Information: 0 : [26780] ConnectStream#26966483 - Sending headers
    {
    API-VERSION: 1
    Host: test.myhost.com
    Content-Length: 329
    Expect: 100-continue
    Connection: Keep-Alive
    }.
    System.Net Information: 0 : [26780] Connection#3888474 - Received status line: Version=1.1, StatusCode=100, StatusDescription=Continue.
    System.Net Information: 0 : [26780] Connection#3888474 - Received headers
    {
    
    }.
    System.Net Information: 0 : [26780] Connection#3888474 - Received status line: Version=1.1, StatusCode=400, StatusDescription=Bad Request.
    System.Net Information: 0 : [26780] Connection#3888474 - Received headers
    {
    0: Content-type
    1: text/xml
    X-Debug-Token: a810dc
    X-Debug-Token-Link: /service/_profiler/a810dc
    Connection: keep-alive
    Content-Length: 3440
    Cache-Control: no-cache
    Content-Type: text/html; charset=UTF-8
    Date: Tue, 14 Apr 2015 11:07:11 GMT
    Server: Apache
    }.


    ...and here's the implementation of the web request:

    private void ButtonSend_Click(object sender, EventArgs e)
            {
                WebHeaderCollection whCol = new WebHeaderCollection();
    
                whCol.Add("API-VERSION", "1");
                //whCol.Add("Content-Type", "text/xml; charset=UTF-8");  <-- That doesn't work in .NET. Content-Type has to be set on the ContentType-Property
    
                string msg = _textBoxReq.Text;
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(_textBoxURL.Text);
    
                byte[] data = Encoding.UTF8.GetBytes(msg);
    
                req.Method = "POST";
                req.ContentType = "text/xml; charset=UTF-8";
                req.ContentLength = data.Length;
                req.Headers = whCol;
    
                req.GetRequestStream().Write(data, 0, data.Length);
    
    
                string xml = "";
                try
                {
                    using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
                    {
                        using (System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream()))
                        {
                            xml = sr.ReadToEnd().Trim();
                        }
                    }
                }
                catch (WebException we)
                {
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(we.Response.GetResponseStream()))
                    {
                        xml = sr.ReadToEnd().Trim();
                    }
                }
                _textBoxRes.Text = xml;
            }

    Can anyone help?

    Thanks,

    MiRi

    Tuesday, April 14, 2015 12:23 PM

Answers

  • ok... found the reason at last...

    The ContentType property belongs to a default WebHeaderCollection. By assigning a new WebHeaderCollection to req.Headers, the content type is being deleted. The solution is to set the value to req.ContentType after setting req.Headers.

    • Marked as answer by _MiRichter Tuesday, April 14, 2015 2:10 PM
    Tuesday, April 14, 2015 2:10 PM

All replies

  • ok... found the reason at last...

    The ContentType property belongs to a default WebHeaderCollection. By assigning a new WebHeaderCollection to req.Headers, the content type is being deleted. The solution is to set the value to req.ContentType after setting req.Headers.

    • Marked as answer by _MiRichter Tuesday, April 14, 2015 2:10 PM
    Tuesday, April 14, 2015 2:10 PM
  • Hi _MiRichter,

    Well Done!

    Thank you very much for sharing the solution to us.

    Best Regards,
    Amy Peng


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, April 15, 2015 3:05 AM
    Moderator