Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

WebRequest & Proxyies

3 views
Skip to first unread message

doug

unread,
Jan 17, 2008, 4:20:04 PM1/17/08
to
I attempted this problem last year and got closer but not close enough. I
actually ended up losing my source I had in place.

I have existing VB6 code that uses WinInets APIs to get outside our firewall
and retrieve a file fro a public source. It hassome very ugly, MS provided
sample, that did retries and such to get the proxy userid and password to
take.

I was attempting that using same APIs as unmanaged calls.

Now I'm back to trying it in VB.Net 2005 with FW v2 using WebRequest.

The code works from my home (no proxy required) to access desired site &
page. The code works inside firewall as long as I ignore proxy calls and
retrieve pages on Intranet.

Solution must support running job as console app on server with a service id
unattended such that the service id credentials are passed to the proxy which
currently lets program out to make the request.

The VB.Net sample I'm using is nice because code is readble and it fits on a
page. The old Vb6 code and VB.Net equivilent (that almost worked) was not
pretty and spanned more pages printed than you might think.

Option Explicit On
Imports System.Net
Imports System.Text

Module Web_GetFile

Sub Main()
Dim result As String = ""
Try
Dim strURL As String = "http://www.public.org/somefile.txt"

Dim credCache As New System.Net.CredentialCache
Dim netCred As New NetworkCredential("muuserid", "mypass",
"mydomain")

Dim proxy As New WebProxy("http://proxy.mycompany.com/", True)
proxy.Credentials = netCred
proxy.BypassProxyOnLocal = True

Dim wb As WebRequest = Nothing
wb = WebRequest.Create(strURL)
wb.Credentials = netCred
wb.PreAuthenticate = "True"
'wb.Method = "POST" 'tried POST and no POST
'wb.Proxy = New System.Net.WebProxy()
wb.Proxy = proxy
wb.Timeout = 10 * 60 * 60


credCache.Add(New Uri(strURL), "Basic", wb.Credentials)


' Send the 'HttpWebRequest' and wait for response.
Dim response As HttpWebResponse = CType(wb.GetResponse(),
HttpWebResponse)

Dim stream As System.IO.Stream = response.GetResponseStream()
Dim ec As System.Text.Encoding =
System.Text.Encoding.GetEncoding("utf-8")
Dim reader As New System.IO.StreamReader(stream, ec)
Dim chars() As Char = New [Char](256) {}
Dim count As Integer = reader.Read(chars, 0, 256)
While count > 0
Dim str = New [String](chars, 0, 256)
result = result + str
count = reader.Read(chars, 0, 256)
End While
response.Close()
stream.Close()
reader.Close()
Catch exp As Exception
Dim str As String = exp.Message
MsgBox(str) ' testing
End Try

End Sub

End Module

0 new messages