none
Portable Class Library and HttpWebRequest.UserAgent RRS feed

  • Question

  • In my W8/WP8 portable class library the user agent of HttpWebRequest is always null. Since the target server requires a user agent I cannot get my code to work as a portable class library.

    I tried to set request.Headers[HttpRequestHeader.UserAgent] but it fails with an exception "The 'User-Agent' header must be modified using the appropriate property or method.\r\nParameter name: name".

    The HttpWebRequest does not have a property called UserAgent! Is there any other way to set the user agent of my webrequest?

    thanks

    Steffen

    Monday, November 5, 2012 1:18 PM

All replies

  • It is a header so you can add it as a header to your webrequest.  The following webpage shows how to add it as a UserAgent Property.  What verion of Net Library are you using?

    http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.useragent.aspx


    jdweng

    Monday, November 5, 2012 2:29 PM
  • Hi Joel,

    as described above, the HttpWebRequest has no public property called UserAgent. If I set the user agend directly to the headers collection (request.Headers[HttpRequestHeader.UserAgent] = "testagent") it throws an exception: "The 'User-Agent' header must be modified using the appropriate property or method.\r\nParameter name: name".

    The target frameworks of the PCL are: .Net for windows store apps; .NET Framework 4.5; Windows Phone 8
    Monday, November 5, 2012 2:38 PM
  • See if this webpage helps

    http://stackoverflow.com/questions/7454301/is-it-possible-to-modify-the-user-agent-for-a-winrt-httpwebrequest

    You may want to go to the webpage use an IE exploreer and view the headers by using menu : View Source to find out what the webpage is expecting.  Sometimes use Wireshark will give additional debug info.


    jdweng

    Monday, November 5, 2012 3:07 PM
  • The webpage I'm calling accepts everything except of a blank user agent header. I have similar code in a windows phone 7 project (using HttpWebRequest) and a WinRT prototype (using HttpClient), both working if the UserAgent is set. Since HttpClient is not available to Portable Class Libraries it is no alternative. The difference between the WP7 HttpWebRequest and PCL implementation is that on WP7 the HttpWebRequest has a UserAgent property that can be set.



    Monday, November 5, 2012 3:32 PM
  • it really doesn't matter if the Net Library has an option to set the UserAgent or you provide the UserAgent as a header.  They both get sent exactly the same from the client to the server.  I'm not sure why Microsoft decided to impliment the net library different on the two operating systems.

    jdweng

    Monday, November 5, 2012 3:43 PM
  • I think the reason are header restrictions on some devices: http://stackoverflow.com/questions/239725/c-sharp-webrequest-class-and-headers

    But I'm not sure why the UserAgent isn't exposed by the HttpWebRequest in a portable class or WinRT library!? This doesn't make any sense since WinRT HttpClient allows the user agent to be set. Hope some of the microsoft guys can help here.


    Monday, November 5, 2012 4:23 PM
  • Hi Gms,

    I am trying to involve some other one in this thread, thank you for your patience.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, November 8, 2012 1:13 PM
    Moderator
  • In order to set UserAgent string on HttpWebRequest object of Windows Phone from a portable library you need to use reflection.

    The following code samples provide a basic example:

            private void SetUserAgent(HttpWebRequest request, string userAgent)

            {

                Type type = request.GetType();

                System.Reflection.PropertyInfo prop = type.GetProperty("UserAgent");

                if (prop != null)

                {

                    prop.SetValue(request, userAgent, null);

                }

            }

            private string GetUserAgent(HttpWebRequest request)

            {

                string ret = "not available";

                Type type = request.GetType();

                System.Reflection.PropertyInfo prop = type.GetProperty("UserAgent");

                if (prop != null)

                {

                    ret = (string)prop.GetValue(request, null);

                }

                return ret;

            }

    Friday, November 9, 2012 11:57 PM
  • Thanks Lou, but this doesn't help. First of all I'm also targeting WinRT.

    The reflection API has changed on this platform. http://blogs.msdn.com/b/dotnet/archive/2012/08/28/evolving-the-reflection-api.aspx

    I changed your code to respect these API changes.

    private void SetUserAgent(HttpWebRequest request, string userAgent)
            {
                var typeInfo = typeof (HttpWebRequest).GetTypeInfo();
                PropertyInfo prop = typeInfo.GetDeclaredProperty("UserAgent");
    
                if (prop != null)
                {
                    prop.SetValue(request, userAgent, null);
                }
            }

    Unfortunately, this approach leads to the following exception:

    System.InvalidOperationException: The API 'System.Net.HttpWebRequest.set_UserAgent(System.String)' cannot be used on the current platform. See http://go.microsoft.com/fwlink/?LinkId=248273 for more information

    My solution for now is to move the web request code to a platform specific library (HttpWebRequest for WP8 and HttpClient for WinRT). I also added a bug to microsoft connect (https://connect.microsoft.com/VisualStudio/feedback/details/770104/cannot-set-useragent-of-a-httpwebrequest-in-portable-class-and-winrt-libraries). Hopefully this functionality will be added in a future release.


    Saturday, November 10, 2012 2:02 PM
  • Thank you for raising this GMS_Steffen, it's a piece of functionality I would also like to see. I've voted accordingly on your bug.
    Wednesday, November 28, 2012 9:37 PM
  • Check out BCL (http://blogs.msdn.com/b/bclteam/archive/2013/02/18/portable-httpclient-for-net-framework-and-windows-phone.aspx). I haven't tested it yet, but it might help!
    Monday, April 29, 2013 10:03 PM
  • I thought I had a similar issue with the Accept header with WebRequest. However casting it to HttpWebRequest solved my issue.

    var

    request = (HttpWebRequest)WebRequester.Create(ApiEndPoint);


    CTO of Hard Medium Soft Ltd. and Independent Developer


    • Edited by Nicholas Rogoff Thursday, April 10, 2014 7:16 PM found solution
    Thursday, April 10, 2014 6:56 PM