locked
Metro app using Web Service calls with UnsafeHeaderParsing

    Question

  • Hi,

    Previous posted and incorrectly send to Off-Topic posts!

    I'm writing a App store C# application which is calling into a third party web service.  However I'd getting an error returned, and it would seem I need to enable using Unsafe Header Parsing.  (Error msg: "The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF").  I'm porting an app from Windows Phone 8 to an App Store app - on Windows Phone 8 the code works 100% and doesn't throw this exception!

    I've looked at the code samples online - all state that I need use app.config settings or Reflection to amend the HttpWebRequest properties to enable  unsafe header parsing - however I am unable to so this for an App Store application. 

    i.e.

    http://social.msdn.microsoft.com/Forums/en-US/ff098248-551c-4da9-8ba5-358a9f8ccc57/how-do-i-enable-useunsafeheaderparsing-from-code-net-20?forum=netfxnetcom

    Given that we developers in the real world often come address web services which aren't 100% compliant with web standards has anyone else come across this issue and successfully resolve it.  Or can provide any pointers?  Or should I abandon all metro development...this issue seems to have been around for nearly 2 years without being addressed. 

    Many thanks

    Monday, June 16, 2014 8:15 PM

Answers

  • Which version of HttpClient class are you using? There are 2 versions of the HttpClient class available for Windows Store apps. The System.Net.Http.HttpClient will give you the same behavior as the System.Net.HttpWebRequest class since they are based on the .NET framework and have their own HTTP Protocol implementation.

    The Windows.Web.Http.HttpClient (notice the different namespace) uses WinINet and has a different implementation than the System.Net classes.

    I wouldn't advise you to use StreamSocket since that means you have to implement the HTTP Protocol yourself.


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    • Marked as answer by lonesheep Tuesday, June 17, 2014 7:39 PM
    Tuesday, June 17, 2014 4:57 PM
    Moderator

All replies

  • Hi,

    I would advise you to look into the new Windows.Web.Http.HttpClient class instead of using the old System.Net.HttpWebRequest class. Using the new Windows.Web.Http.HttpClient class will give you the behavior that WinINet (or IE) gives you.

    Thanks


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    Tuesday, June 17, 2014 12:45 AM
    Moderator
  • Hi,

    I also tried using the HttpClient class;

    try
    {
        string page = "http://192.168.1.123/cgi-bin/thumbNail?fn=/www/sd/DCIM/101_FUJI/DSCF1048.JPG";
        using (HttpClient client = new HttpClient())
        using (HttpResponseMessage response = await client.GetAsync(page))   // <-- Exception thrown here
        using (HttpContent content = response.Content)
        {
           Stream result = await content.ReadAsStreamAsync();

        }
    }
    catch (Exception ex)
    {
        // Exception is "The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF"
    }

    I note that you say this should have the same behaviour that IE give me, however I can browse the Url within IE on the tablet with the correct response.

    I started looking at using a StreamSocket object, are there any examples of performing a HTTP POST using this?  My attempt seems to do nothing but wait and return nothing, but then again I haven't used sockets before;

    string hostname = "192.168.1.123";
    string port = "80";

    StreamSocket ImapSer = new StreamSocket();
    await ImapSer.ConnectAsync(new HostName(hostName), port);

    string str = "POST /cgi-bin/thumbNail?fn=/www/sd/DCIM/101_FUJI/DSCF1048.JPG HTTP/1.1\r\n";

    Byte[] flag = new Byte[1024];
    flag=System.Text.Encoding.UTF8.GetBytes(str);

    // Set up the reader to wait for the response
    DataReader sr1 = new DataReader(ImapSer.InputStream);
    sr1.InputStreamOptions = InputStreamOptions.Partial;

    // Send request
    DataWriter writer = new DataWriter(ImapSer.OutputStream);
    writer.WriteString(str);
    await writer.StoreAsync();

    // Wait for response
    var stringHeader = await sr1.LoadAsync(512);
    string data2 = sr1.ReadString(sr1.UnconsumedBufferLength);

    Many thanks

    Tuesday, June 17, 2014 5:05 AM
  • Which version of HttpClient class are you using? There are 2 versions of the HttpClient class available for Windows Store apps. The System.Net.Http.HttpClient will give you the same behavior as the System.Net.HttpWebRequest class since they are based on the .NET framework and have their own HTTP Protocol implementation.

    The Windows.Web.Http.HttpClient (notice the different namespace) uses WinINet and has a different implementation than the System.Net classes.

    I wouldn't advise you to use StreamSocket since that means you have to implement the HTTP Protocol yourself.


    Windows Store Developer Solutions, follow us on Twitter: @WSDevSol|| Want more solutions? See our blog

    • Marked as answer by lonesheep Tuesday, June 17, 2014 7:39 PM
    Tuesday, June 17, 2014 4:57 PM
    Moderator
  • Many thanks, using Windows.Web.Http.HttpClient  has sorted it out.

    Cheers

    Tuesday, June 17, 2014 7:40 PM