Set user agent header with webrequest

Discussion in 'ASP .Net' started by Jon Spivey, Nov 20, 2004.

  1. Jon Spivey

    Jon Spivey Guest

    Hi,

    I'm using webrequest to scrape some urls, works fine, however one url seems
    to be throwing an error when it doesn't recognise the user agent. So I need
    to set the user agent to something like MSIE, I've tried these
    WebRequest.Headers.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0;
    Windows NT 5.1;)")
    WebRequest.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0;
    Windows NT 5.1;)")
    WebRequest.Headers.Item("User-Agent") = "Mozilla/4.0 (compatible; MSIE 6.0;
    Windows NT 5.1; )")
    and they all give the same error "System.ArgumentException: This header must
    be modified with the appropriate property"

    What's the correct way to set the User Agent header?

    Cheers,
    Jon
     
    Jon Spivey, Nov 20, 2004
    #1
    1. Advertisements

  2. Jon Spivey

    jongalloway Guest

    jongalloway, Nov 20, 2004
    #2
    1. Advertisements

  3. Jon Spivey

    tjrobinson

    Joined:
    Oct 1, 2009
    Messages:
    1
    Likes Received:
    0
    There's no WebRequest.UserAgent property, but there is a HttpWebRequest.UserAgent property. You can use:

    Code (Text):

    ((HttpWebRequest)request).UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
    Source: Search for "How to: Request Data Using the WebRequest Class" on MSDN (I can't post links as I'm new to this forum)
     
    tjrobinson, Oct 1, 2009
    #3
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. LIN

    User Agent

    LIN, Aug 14, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    675
    James J. Foster
    Aug 14, 2003
  2. Colin
    Replies:
    0
    Views:
    452
    Colin
    Dec 1, 2003
  3. Brian Henry

    retrieveing entire user agent

    Brian Henry, Dec 22, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    477
    Brian Henry
    Dec 22, 2003
  4. Brian Henry

    user agent return on a local machine

    Brian Henry, Dec 26, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    443
    Carl Prothman [MVP]
    Dec 26, 2003
  5. \Rob\
    Replies:
    1
    Views:
    837
    Peter van der Goes
    Jun 29, 2004
  6. Replies:
    6
    Views:
    892
  7. mlt
    Replies:
    2
    Views:
    1,153
    Jean-Marc Bourguet
    Jan 31, 2009
  8. Luke Matuszewski
    Replies:
    8
    Views:
    872
    Luke Matuszewski
    Dec 2, 2005
Loading...