locked
WCF REST Starter Kit HttpClient issues RRS feed

  • Question

  • I am attempting to use the HttpClient in the WCF REST Starter Kit. 

    HttpClient hc = new HttpClient();
    
    HttpContent content = HttpContent.Create(xmlRequest, Encoding.UTF8, "text/xml");
    
    HttpResponseMessage hrm = hc.Post("https://www.whatever.com, "text/xml", content);
    

    xmlRequest contains the XML I am posting to the RESTful service. The URL has https as you can see. Whenever I do the post I get back "InternalServerError 500". Is there something special I need to do since it is https?
    Monday, May 18, 2009 9:04 PM

Answers

  • Solved the problem. These need to be set:

                    System.Net.ServicePointManager.Expect100Continue = true;
                    hc.TransportSettings.SendChunked = false;
                    hc.TransportSettings.PreAuthenticate = false;
    
    where hc is the HttpClient object

    Also must be sent in ASCII encoding. UTF8 should work as they should be totally backwards compatible for the character range being used but it does not.
    Wednesday, May 20, 2009 4:31 PM

All replies

  • 1) Are you sure that there is nothing wrong with the service?  Can you debug your request at the service side and see what is going on?
    2) Do you get the same response when you use HttpWebRequest?

    I tried the following and I got a Http 200

    var res = new HttpClient().Post("https://www.xxx.com", HttpContentExtensions.CreateDataContract<string>("Hello"));
    Monday, May 18, 2009 10:45 PM
  • do you get that an an exception or in the HttpResponseMessage? If the latter is there any extra information in the response?
    Richard Blewett, thinktecture - http://www.dotnetconsult.co.uk/weblog2
    Twitter: richardblewett
    Tuesday, May 19, 2009 7:07 AM
  • In addition to what Amit and Richard suggested, I would also check whether the service certificate is valid. The http stack on the client side will throw an exception if that certificate is invalid (unless you disable the certificate validation).

    Regards,
    Pablo.
    Pablo Cibraro - http://weblogs.asp.net/cibrax
    Tuesday, May 19, 2009 12:53 PM
  • 1) Are you sure that there is nothing wrong with the service?  Can you debug your request at the service side and see what is going on?
    2) Do you get the same response when you use HttpWebRequest?

    I tried the following and I got a Http 200

    var res = new HttpClient().Post("https://www.xxx.com", HttpContentExtensions.CreateDataContract<string>("Hello"));
    1) The service definitely works. I cannot debug it at the service side. The service is supplied by another company.
    2) No, HttpWebRequest works just fine. I would like to use the new HttpClient object if possible, however.

    do you get that an an exception or in the HttpResponseMessage? If the latter is there any extra information in the response?
    It is what is returned ("InternalServerError 500") in the HttpResponseMessage. That's all it says.
    In addition to what Amit and Richard suggested, I would also check whether the service certificate is valid. The http stack on the client side will throw an exception if that certificate is invalid (unless you disable the certificate validation).
    Cert is valid. Thanks for the replies so far.
    Tuesday, May 19, 2009 3:04 PM
  • The default for "text/xml" is US-ASCII per http://tools.ietf.org/html/rfc3023#section-8.5 . So, specifying Encoding.UTF8 and "text/xml" may not do what you want. I would suggest either HttpContentExtensions.Create(XElement.Parse(xmlRequest)) (which will use "application/xml" and UTF8) or HttpContentExtensions.Create(XElement, Encoding.ASCII, "text/xml") or HttpContentExtensions.Create(XElement, Encoding.UTF8, "text/xml; charset=\"utf-8\""). (Also, you don't need to specify the contentType again in the Post(): it'll automatically pick it up from the content.)

    If you are still having issues, please include the XML string and the HttpWebRequest code that works. (HttpWebRequest sets fewer headers than HttpClient so there could be something there: compare the response.Request.Headers to the webRequest.Headers after you webRequest.GetResponse().)

    If the URL or XML contains sensitive information that you don't want to post, please log a bug via Connect: https://connect.microsoft.com/VisualStudio/content/content.aspx?ContentID=12361 .


    John Lambert - JLamb - Microsoft
    Tuesday, May 19, 2009 5:18 PM
  • Solved the problem. These need to be set:

                    System.Net.ServicePointManager.Expect100Continue = true;
                    hc.TransportSettings.SendChunked = false;
                    hc.TransportSettings.PreAuthenticate = false;
    
    where hc is the HttpClient object

    Also must be sent in ASCII encoding. UTF8 should work as they should be totally backwards compatible for the character range being used but it does not.
    Wednesday, May 20, 2009 4:31 PM