How to send WebRequest via proxy?

痞子三分冷 提交于 2019-12-21 05:39:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!