CJ7
CJ7

Reputation: 23295

How to send WebRequest via proxy?

How does the following code need to be modified to send the WebRequest via a specified proxy server and port number?

Dim Request As HttpWebRequest = WebRequest.Create(url)
Request.Method = "POST"
Request.ContentType = "application/x-www-form-urlencoded"
Using writer As StreamWriter = New StreamWriter(Request.GetRequestStream())
    writer.Write(params)
End Using

Upvotes: 3

Views: 23545

Answers (3)

Bhavik Patel
Bhavik Patel

Reputation: 772

For example, if your Web server needs to go through the proxy server at http://255.255.1.1:8080,

Dim Request As HttpWebRequest = WebRequest.Create(url)
 'Create the proxy class instance
Dim prxy as New WebProxy("http://255.255.1.1:8080")

 'Specify that the HttpWebRequest should use the proxy server
Request .Proxy = prxy

Request.Method = "POST"
Request.ContentType = "application/x-www-form-urlencoded"
Using writer As StreamWriter = New StreamWriter(Request.GetRequestStream())
writer.Write(params)
End Using

Upvotes: 0

javad helali
javad helali

Reputation: 270

use this code from MSDN :

Dim myProxy As New WebProxy()
myProxy.Address = New Uri("proxyAddress")
myProxy.Credentials = New NetworkCredential("username", "password")
myWebRequest.Proxy = myProxy

Upvotes: 7

Darren Reid
Darren Reid

Reputation: 2322

A WebRequest object has a 'Proxy' property of IWebProxy. You should be able to assign it to use a specified proxy.

Request.Proxy = New WebProxy("http://myproxy.com:8080");

If the proxy is not anonymous, you will need to specify the Credentials of the WebProxy object.

Upvotes: 4

Related Questions