2

I have created a class within my PCL to get the needed Session-Id and a Key for login into a webserver. (The response is XML, which is parsed in a second method which runs the Login) I have no control on the WebServer, so I need to solve my problem on the client side.

This is my Task Method:

public static async Task<string> SessionId()
{
    HttpWebRequest SessiondIdRequest = (HttpWebRequest)WebRequest.Create(Constants.SessionIdUrl);

    SessiondIdRequest.Method = "POST";
    SessiondIdRequest.Accept = "application/xml";
    SessiondIdRequest.Headers[HttpRequestHeader.AcceptLanguage] = "de-de,de;q=0.8,en-us;q=0.5,en;q=0.3";
    SessiondIdRequest.ContentType = "application/x-www-form-urlencoded ";


    string appIDviaPOST = "{Here is a String}";
    byte[] byteArray = Encoding.UTF8.GetBytes(appIDviaPOST);

    using (var SessionIdRequestStream = await Task<Stream>.Factory.FromAsync
        (SessiondIdRequest.BeginGetRequestStream, SessiondIdRequest.EndGetRequestStream, SessiondIdRequest))
    {
        await SessionIdRequestStream.WriteAsync(byteArray, 0, byteArray.Length);
    }

   WebResponse SessionIdRequestResponse = await Task<WebResponse>.Factory.FromAsync
       (SessiondIdRequest.BeginGetResponse, SessiondIdRequest.EndGetResponse, SessiondIdRequest);
   var SessionIdRequestResponseStream = SessionIdRequestResponse.GetResponseStream();
    var sr = new StreamReader(SessionIdRequestResponseStream);
    string SessionIdResult = await sr.ReadToEndAsync();

    return SessionIdResult;
}

The Task works very well on WP7 & WP8. I am getting a totally different result when I am using this Method in the Windows 8 part of my app.

I used Fiddler to find out the differences between the two, the most markable thing I saw that WP7&8 are using HTTP 1.0 and send an UserAgent, whereas Win8 uses HTTP 1.1 and does not send an UserAgent.

In PCL, the HttpWebRequest does not contain properties for UserAgent and ProtocolVersion, so I am not able to tell the Method to force it (because it is a subset only).

Is there any way to work around this (besides to copy the whole method into my Win8 project, where I am able to set those properties).

I am very new to MVVM and PCL, so please be a bit forgiving it this is a dumb question for you. Thanks.

theB
  • 6,450
  • 1
  • 28
  • 38
MSicc
  • 329
  • 3
  • 10

3 Answers3

2

I solved a very similar problem by switching to the new HttpClient (PCL) library and setting the Version property there. Search on NuGet for 'Microsoft HTTP Client Libraries' (beta as of today but working pretty well).

I still don't understand why it always works for me by using 1.0 and just sporadically (Win7 and Win8) when using 1.1

ForceMagic
  • 6,230
  • 12
  • 66
  • 88
ThumbGen
  • 162
  • 2
  • 8
0

Believe it or not it is not possible to change the UserAgent header when using PCL.

Read the comments on this thread:

Posted by Microsoft on 2/25/2013 at 4:05 PM

Regretfully,the portable library design has been to support only properties that are globally available-- and the user agent cannot be set on all platforms (e.g., for SilverLight).

Unfortunately, most of the restricted headers are not PCL, so it cannot be changed on most platform.

As I seen on this thread, the only way to change those restricted headers is to use reflection.

var httpRequest = (HttpWebRequest) WebRequest.Create(uri);

var userAgent = httpRequest.GetType().GetProperty("UserAgent");
if(null != userAgent) 
{
    userAgent.SetValue(httpRequest, "Your value", null);
}

Althought, this is far from ideal because even if it might work on some platform, on some others you will get an error similar to this:

System.InvalidOperationException: The API System.Net.HttpWebRequest.set_UserAgent(System.String)' cannot be used on the current platform. (see this post)

ThumbGen's answer could be another alternative if your project match with the supported platform, but I haven't tried it yet.

Community
  • 1
  • 1
ForceMagic
  • 6,230
  • 12
  • 66
  • 88
-2

you can try setting the user-agent header, if you are not able to find the User-Agent property.

SessiondIdRequest.Headers["User-Agent"] = @"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)";

You could also try constructing the request header, Instead of using the provided properties.

Key              Value
Request          POST http://localhost/index.aspx HTTP/1.0
Content-Length  *length*
Vignesh.N
  • 2,618
  • 2
  • 25
  • 33
  • I did that and it was returned with Miscellaneous, not Client. Also, I think the main problem is the HTTP version. Thanks anyways for your answers. – MSicc Mar 25 '13 at 06:08
  • did not work, I am figuring out another solution. will post it as soon as possible – MSicc Mar 25 '13 at 20:13
  • Unfortunately, trying to change `Headers` using `["User-Agent"]` lead to this problem: http://stackoverflow.com/q/239725/62921 – ForceMagic Dec 31 '13 at 19:52