vb.nethttpwebresponsegetresponse

GetResponse not working for internal url


I am trying to return a webpage as a stream.

What i have got so far works when i try and access an external url i.e. 'http://www.google.com'. But when i try and access a website on our server i.e. 'http://servername/applicationname/default.aspx'.

Below is the code that i currently have, that works for external :

Dim request As HttpWebRequest = CType(WebRequest.Create("http://www.google.com/search?q=google"), HttpWebRequest)
Dim response As HttpWebResponse = CType(request.GetResponse, HttpWebResponse)
Dim resStream As Stream = response.GetResponseStream

But when i try it with this line i get The remote server returned an error: (401) Unauthorized.

Dim request As HttpWebRequest = CType(WebRequest.Create("http://Server/Application/SubFolder/testing.aspx"), HttpWebRequest)        
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)

I have tried altering the credentials it accesses the server with using :

request.Credentials = CredentialCache.DefaultCredentials

And i have tried using a web client :

Dim myWebClient As New WebClient()
Dim myStream As Stream = myWebClient.OpenRead("http://Server/Application/SubFolder/testing.aspx")

Solution

  • You're getting the 401 error because the web server's authentication is rejecting the request. You may need to explicitly set your credentials.

                request.Credentials = New NetworkCredential("userName", "password", "domain")