2012-11-19 75 views
2

需要如何修改以下代碼才能通過指定的proxy serverport number發送WebRequest如何通過代理髮送WebRequest?

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 

回答

4

WebRequest對象具有IWebProxy的'Proxy'屬性。您應該能夠將其分配給使用指定的代理。

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

如果代理不是匿名的,則需要指定WebProxy對象的憑證。

7

使用此代碼從MSDN:

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

例如,如果你的Web服務器需要通過在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