Answered Supplying data in User-Agent field of HTTP GET request

j_wolfe

Member
Joined
May 25, 2009
Messages
5
Programming Experience
Beginner
Hello,
I am writing a program that makes use of random.org's random number generator, and one guideline from the site is "Configure your client to supply your email address in the User-Agent field of the request. That way, I can drop you a line if your client is causing trouble." How do I do this? This is what I have, it works fine, but I can't figure out how to put stuff in the "User-Agent" field.
VB.NET:
    Private Function HardwareStartNote() As Integer
        Dim sURL As String
        sURL = "http://www.random.org/integers/?num=1&min=0&max=11&col=1&base=10&format=plain&rnd=new"

        Dim wrGETURL As WebRequest
        wrGETURL = WebRequest.Create(sURL)

        Dim objStream As Stream
        objStream = wrGETURL.GetResponse.GetResponseStream()

        Dim objReader As New StreamReader(objStream)
        Dim sLine As String
        sLine = objReader.ReadLine
        Return sLine
    End Function
This is my first time using http requests in programming, and i'm working mostly off examples, so please point out anything wrong or unnecessary.

This was supposed to be put in Visual Basic.NET, sorry.
 
Last edited:
VB.NET:
Dim req As Net.HttpWebRequest = CType(Net.WebRequest.Create("http://..."), Net.HttpWebRequest)
req.UserAgent = "some@email"
As explained in help WebRequest is a base class, the actual instance type depends on the protocol used, in this case HttpWebRequest. Normally headers are added through the Headers property, except those listed in help that has dedicated property accessors, such as the UserAgent property.
 
Also Close the reader when done, before your function returns, this will close the stream and connection used by the request.
 
Back
Top