HttpWebRequest not sending UserAgent and IP-adress of client.

Answered HttpWebRequest not sending UserAgent and IP-adress of client.

  • Thursday, September 20, 2012 5:58 PM
     
     

    When developing a solution that POST's data to a server and receives a response back, the server needs to parse the clients IP-adress and User Agent. I'm a bit intrigued by the fact that HttpWebRequest in Windows 8 doesn't behave as HttpWebRequest in Windows Phone. The server receives a proper request but there is no User Agent and IP-adress included in the request which causes this vital server call to fail (works in iOS, Android and Windows Phone though).

    I then was told that this might be due to the "Do Not Track"-setting in IE10 but even though I've disabled that setting (and restarted the computer, wow how 90's :) ) the app still fails to send these properties.

    Any idea what's causing this behavior and if there is a safe (from a certification perspective) way to send this information?

    I've tried using the HttpClient instead but I can't figure out the proper way to write to both the request stream as well as read from the response stream properly (since I have to use ProtoBuf).

    Please advise!

    Regards

    Johan Lindfors

All Replies

  • Friday, September 21, 2012 4:53 PM
    Moderator
     
     
    It sounds like you may have to set these cookies yourself.  Please see this post:
    http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/3724d52e-c92a-45c7-8ed5-8e76a244d80b

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


  • Saturday, September 22, 2012 7:24 AM
     
     

    Hi Matt and thanks for taking the time to try and help me out...

    This has grown to be quite difficult since using HttpWebRequest or HttpClient generates different behaviors which I can't seem to overcome:

    More requirements: I need to send two Headers: "User-Agent" and "Cookie". User-Agent can be any arbitrary value but NOT missing. Cookie needs to be a specific authentication-token which is actually a string representing the ticket to log in (generated in earlier login-request and cached in the client).

    1. Using HttpWebRequest I manage to provide the Cookie successfully (as a custom header with a custom value) but there's no possibility to specify the User-Agent

    2. Using HttpClient I can specify the User-Agent but I need to UrlEncode the value of the authentication-token to successfully be able to add it to the cookie-container attached to the HttpMessageHandler which gets provided to the HttpClient. But then the authentication fails on the server. If I don't UrlEncode the token an exception gets thrown due to bad cookie value.

    Any recommendations?

    I have a hard time requesting the server to reimplement their authentication mechanism since this works perfectly on iOS, Android and Windows Phone, it's just WinRT that's causing this error.

    Please advise.

    Johan Lindfors


  • Monday, September 24, 2012 8:29 PM
     
     Answered

    This has now been solved by examining the potential constructors for Cookie which happened to match the parts of the AuthenticationToken perfectly and when that token was divided into its parts, they generated a valid Cookie and I'm now authenticated and a correct User-Agent has been sent as well.

    I was blinded by the fact that with HttpWebRequest I simply created a "custom" Http-header with the name "Cookie" and the value was the exact token...

    Or as I like to mention it: My Bad!

    Regards

    Johan Lindfors

  • Tuesday, September 25, 2012 1:17 PM
    Moderator
     
     
    I'm glad to hear it.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.