HttpWebRequest.getResponse exception not understood

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
Im new to Visual Studio 2010 and am using Visual Basic.
I have done a tutorial on creating a simple web browser with the WebBrowser Class. And it works great :) I have also done tutorials using webClient, HttpWebRequest and HttpWebresponse to gather page data and other things as well with success :)
I have a webserver that displays a stupidly simple test page in html and using IE, FireFox, Chrome etc I can type in the IP address (http://192.168.05 - a local address) and up comes the page :) This even happens in that web browser I made in the tutorial :)
Im trying this: (in a form with a button to go to the address in a textbox and a rich textbox to display the results)Get Page Button
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim _WebContents As String = Nothing
Read HTML contents from a web page
_WebContents = DownloadHTMLPage("http://" & TextBox1.Text.Trim)
Show HTML contents in RichTextBox
If _WebContents IsNot Nothing Then
RichTextBox1.Text = _WebContents
End If
End Sub

Get Page Button Function
Public Function DownloadHTMLPage(ByVal _URL As String) As String
Dim _PageContent As String = Nothing
Try
Open a connection
Dim _HttpWebRequest As System.Net.HttpWebRequest = CType(System.Net.HttpWebRequest.Create(_URL), System.Net.HttpWebRequest)
You can also specify additional header values like the user agent or the referer: (Optional)
_HttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)"
_HttpWebRequest.Referer = "http://www.google.com/"
set timeout for 10 seconds (Optional)
_HttpWebRequest.Timeout = 10000
Request response:
Dim _WebResponse As System.Net.WebResponse = _HttpWebRequest.GetResponse()
Open data stream:
Dim _WebStream As System.IO.Stream = _WebResponse.GetResponseStream()
Create reader object:
Dim _StreamReader As New System.IO.StreamReader(_WebStream)
Read the entire stream content:
_PageContent = _StreamReader.ReadToEnd()
Cleanup
_StreamReader.Close()
_WebStream.Close()
_WebResponse.Close()
Catch _Exception As Exception
Error
MsgBox(_Exception.Message)
Return Nothing
End Try
Return _PageContent
End Function
I will use the local address and get an exception that says the response Header Name from the server is invalid even though the webbrowser can display it as Ive stated above. The header sent has "HTTP/1.1 200 OK"+chr(13)+chr(10)+chr(10)+"Content-type: text/html"+(html document, that simple webpage)
The exception happens (when I remove the try statements) at the "getResponse" part.
Can anyone tell me what the exception is talking about? Im stuck and want to understand it.
Thanks
Chris

View the full article
 
Back
Top