Set user agent header with webrequest

J

Jon Spivey

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
 
Joined
Oct 1, 2009
Messages
1
Reaction score
0
There's no WebRequest.UserAgent property, but there is a HttpWebRequest.UserAgent property. You can use:

Code:
((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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top