好的,所以我是一个很长一段时间潜伏的第一次海报。在发布之前,我已经对此进行了相当多的研究,但作为一名自学成才的程序员,我常常迷失在技术术语中。
我有一些VB.NET(Framework 4)应用程序,总共有大约1,400个用户,具有各种网络配置。应用程序需要连接到一些可公开访问的Web URL,我使用WebRequest。大多数用户都没有任何问题,但代理人背后的少数(通常是较大的公司)也是如此。因此,我将允许用户指定其代理设置(下面代码中的文本框)。
这是我到目前为止的函数,它将Request.GetResponse()作为我可以查询的字符串返回。
Public Function WebRequest(ByVal Address As Uri, ByVal Timeout As Integer) As Net.WebResponse
Net.ServicePointManager.DefaultConnectionLimit = Net.ServicePointManager.DefaultConnectionLimit + 1
Net.ServicePointManager.MaxServicePointIdleTime = 60000
Dim Request As Net.WebRequest = Net.WebRequest.Create(Address)
Request.CachePolicy = New Net.Cache.HttpRequestCachePolicy(Net.Cache.RequestCacheLevel.NoCacheNoStore)
Request.Timeout = Timeout
Request.Proxy = Net.WebRequest.GetSystemWebProxy()
If frmSettings.txtUsername.Text = Nothing OrElse frmSettings.txtPassword.Text = Nothing Then
Request.UseDefaultCredentials = True
Else
Request.Proxy.Credentials = New Net.NetworkCredential(frmSettings.txtUsername.Text, frmSettings.txtPassword.Text)
End If
Return Request.GetResponse
End Function
提前致谢! :)
答案 0 :(得分:1)
使用此命令获取系统代理并分配到您的Web请求
Dim wReq As HttpWebRequest = DirectCast(WebRequest.Create("yoururl"), HttpWebRequest)
Dim px As IWebProxy = WebRequest.GetSystemWebProxy()
wReq.Proxy = px
wReq.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials
wReq.Credentials = CredentialCache.DefaultNetworkCredentials