简体   繁体   English

如何通过代理发送WebRequest?

[英]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 ? 如何通过指定的proxy serverport number修改以下代码以发送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

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

A WebRequest object has a 'Proxy' property of IWebProxy. WebRequest对象具有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. 如果代理不是匿名的,则需要指定WebProxy对象的凭据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM