'download a file through proxy' code problem

K

kieran

Hi,

I am trying to download a file from the Internet. The below code
works fine with no proxy server but I am behind a proxy server at work
and need to figure out how to include it in the code.

<code>
Dim wc As New System.Net.WebClient
wc.DownloadFile("http://www.google.ie/images/hp0.gif", _
"C:\inetpub\wwwroot\bin\google.gif")
</code>



I have been looking all over google and am trying the following code
but as i am not familar with vb.net i can not get the structure right
or am not sure if this is the way to do it.


<code>
dim ProxyObject as New WebProxy= new
webProxy(Http://proxyserverIP:8080,true)
dim cred as NetworkCredential= new
NetworkCredential("User","password","domain")
ProxyObject.Credentials=cred
GlobalProxySelection.Select=proxyObject
<code>


Thanks for any and all help.
 
P

Patrick Steele [MVP]

kieran5405 said:
Hi,

I am trying to download a file from the Internet. The below code
works fine with no proxy server but I am behind a proxy server at work
and need to figure out how to include it in the code.

<code>
Dim wc As New System.Net.WebClient
wc.DownloadFile("http://www.google.ie/images/hp0.gif", _
"C:\inetpub\wwwroot\bin\google.gif")
</code>



I have been looking all over google and am trying the following code
but as i am not familar with vb.net i can not get the structure right
or am not sure if this is the way to do it.


<code>
dim ProxyObject as New WebProxy= new
webProxy(Http://proxyserverIP:8080,true)
dim cred as NetworkCredential= new
NetworkCredential("User","password","domain")
ProxyObject.Credentials=cred
GlobalProxySelection.Select=proxyObject
<code>

I've never gove through a proxy, but the above code looks correct.

However, the WebClient class doesn't have a Proxy property but the
WebRequest class does. You may have to use the WebRequest object and
stream the data into the file yourself.
 
P

Patrick Steele [MVP]

kieran5405 said:
Hi Patrick,

Cheers for response.

I have been trying what you said and can get the stream to read but
can not see how to write it. I am using the following code and keep
getting the error -

"Stream was not writable."
<code>

Dim sURL As String
sURL = "http://test.com/rss.xml"

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

Dim saByPassList() As String
Dim myProxy As New WebProxy("proxy:8080", True, saByPassList, New
NetworkCredential("user", "password", "_domain"))

myProxy.BypassProxyOnLocal = True

wrGETURL.Proxy = myProxy

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

Dim myFileStream As New StreamWriter(objStream)

I think you want to try a StreamReader instead of a StreamWriter (read
from the response stream of the webrequest).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top