Saturday, March 24, 2012

Setting the "User-Agent" Header --Please help!

I need to set the User-Agent of HttpWebRequest, but when trying this:
req.Headers.Add("User-Agent", "Mozilla/4.0")
or this:
req.Headers.Set("User-Agent", "Mozilla/4.0")
I get this error:
"This header must be modified with the appropriate property"

So what's the appropriate property?

(btw: it's the same problem this guy had: http://www.mcse.ms/message637287.html)The AddHeader or AppendHeader of the HttpResponse class:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwebhttpresponsememberstopic.asp
but I'm using the WebRequest and WebResponse classes.
Here's the code:


dim Page as string = "http://www.yahoo.com"

Dim HTMLas string
Dim objURI as System.URI
Dim objWebRequestas System.Net.WebRequest
Dim objWebResponseas System.Net.WebResponse
Dim objStreamas System.IO.Stream
Dim objStreamReaderas System.IO.StreamReader

objURI = New URI( Page )
objWebRequest = System.Net.WebRequest.Create(objURI)

'objWebRequest.headers.set("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)") THIS GETS ME AN ERROR

objWebResponse = objWebRequest.GetResponse()
objStream = objWebResponse.GetResponseStream()
objStreamReader = New System.IO.StreamReader(objStream)

HTML = objStreamReader.ReadToEnd

objWebResponse.Close()
objStream.Close()
objStreamReader.close()


I don't think you can do it that way.
so, in what way can I do it? As you can see I'm just reading a page.

I need this because of an API that needs you to be a specific agent, the "unknown" agent is rejected --quite dumb, if you ask me.
As far as I know you can do it in PHP.
I found it. Change the ".NET Framework Test Client" to whatever your user-agent header is:

' Create a new 'HttpWebRequest' object to the mentioned URL.
Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://www.contoso.com"), HttpWebRequest)
myHttpWebRequest.UserAgent= ".NET Framework Test Client"

' The response object of 'HttpWebRequest' is assigned to a 'HttpWebResponse' variable.
Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)

' Display the contents of the page to the console.
Dim streamResponse As Stream = myHttpWebResponse.GetResponseStream()
Dim streamRead As New StreamReader(streamResponse)
Dim readBuff(256) As [Char]
Dim count As Integer = streamRead.Read(readBuff, 0, 256)
Console.WriteLine(ControlChars.Cr + "The contents of HTML Page are :" + ControlChars.Cr)

While count > 0
Dim outputData As New [String](readBuff, 0, count)
Console.Write(outputData)
count = streamRead.Read(readBuff, 0, 256)
End While

streamRead.Close()
streamResponse.Close()

' Release the response object resources.
myHttpWebResponse.Close()
wow!
thank you, I'll try that.

0 comments:

Post a Comment