locked
How to get the user-agent with C#? RRS feed

  • Question

  • Hi All,

    I have a question about how to get the client user-agent with C#? I have tried the code flowering,but it does not work.

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
    string agent = request.UserAgent;

    Any suggestions?

    Thanks,

    Leslu

    Friday, August 29, 2014 7:43 AM

Answers

  • The UserAgent string which you get with javascript is generated by IE. If you want to generate the same string, the easist way is to copy that string into your Console Application. Another way is to read these properties from the registry. Generate the tokens based on the OS information and some application information.

    Please read this documentation:

    Understanding user-agent strings

    You can find the registry entry in this documentation, and the needed tokens in the UserAgent string.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Leslu Tuesday, September 2, 2014 12:47 AM
    Monday, September 1, 2014 2:54 AM

All replies

  • Web api?

    It's stored in the webheadercollection.

    You could try:

    if (Request.Headers.Contains("User-Agent"))
    {
        var headers = request.Headers.GetValues("User-Agent");
    
        StringBuilder sb = new StringBuilder();
    
        foreach (var header in headers)
        {
            sb.Append(header);
    
            // Re-add spaces stripped when user agent string was split up.
            sb.Append(" ");
        }
    
        userAgent = sb.ToString().Trim();
    }


    Hope that helps
    Please don't forget to up vote answers you like or which help you and mark one(s) which answer your question.

    • Proposed as answer by cnk_gr Friday, August 29, 2014 4:19 PM
    Friday, August 29, 2014 3:49 PM
  • Mm,how to invoke "Request" , I have  input the system.web.UI ,but not find this one.
    Sunday, August 31, 2014 1:03 AM
  • Hi Leslu,

    What kind of application are you working on?

    When a web client like IE/Firefox/Chrome web browser send a request to the web server, it'll automatically send the User-Agent header to the server. Then on the server side, if you're using ASP.NET web application, the HttpContext.Request will contains the User-Agent header, and you could use Andy's method to get the value.

    Basically the thing is that the web client set the request headers like User-Agent or Content-Type, the web server receive it and resolve it. You create a HTTP request in your application, you're on the client site, what you need to do is to set the User-Agent, but not to receive it. So if you send this request to your own web server, for example, if it's an ASP.NET web application, in the ASP.NET page handler, you could use the Request.UserAgent to get the value you've set.

    I recommend that you read this document talking about HTTP protocol:

    http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Sunday, August 31, 2014 4:21 AM
  • Thanks Caillen. I do it in a console application. Is it possible to get the user-agent string from local machine? Just like input "javascript:alert(navigator.userAgent)" in the browser address. I want to get the value from C#.

    

    Monday, September 1, 2014 1:39 AM
  • The UserAgent string which you get with javascript is generated by IE. If you want to generate the same string, the easist way is to copy that string into your Console Application. Another way is to read these properties from the registry. Generate the tokens based on the OS information and some application information.

    Please read this documentation:

    Understanding user-agent strings

    You can find the registry entry in this documentation, and the needed tokens in the UserAgent string.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    • Marked as answer by Leslu Tuesday, September 2, 2014 12:47 AM
    Monday, September 1, 2014 2:54 AM