.net 如何通过代理发送 WebRequest?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13447495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 16:43:04  来源:igfitidea点击:

How to send WebRequest via proxy?

.netproxyhttpwebrequestport

提问by CJ7

How does the following code need to be modified to send the WebRequestvia a specified proxy serverand port number?

以下代码需要如何修改才能WebRequest通过指定的proxy server和发送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

回答by javad helali

use this code from MSDN :

使用 MSDN 中的此代码:

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

回答by Darren Reid

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

WebRequest 对象具有 IWebProxy 的“代理”属性。您应该能够分配它以使用指定的代理。

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.

如果代理不是匿名的,您将需要指定 WebProxy 对象的 Credentials。

回答by Bhavik Patel

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

例如,如果您的 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