问题
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
回答1:
use this code from MSDN :
Dim myProxy As New WebProxy()
myProxy.Address = New Uri("proxyAddress")
myProxy.Credentials = New NetworkCredential("username", "password")
myWebRequest.Proxy = myProxy
回答2:
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.
回答3:
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
来源:https://stackoverflow.com/questions/13447495/how-to-send-webrequest-via-proxy