You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为HttpWebResponse添加代理?附WebRequest代理配置示例

Configuring Proxy for HttpWebResponse (via HttpWebRequest)

Great question—let’s start with a key clarification: proxies are configured on the HttpWebRequest that generates the HttpWebResponse, not the response itself. The response is just the server's reply after your request (routed through the proxy) completes. Here’s how to integrate proxy settings into your MultipartFormDataPost function, building on the approach you already use for standard WebRequest calls:

Step-by-Step Implementation

  1. Locate the HttpWebRequest creation in your function
    Your MultipartFormDataPost method must be creating an HttpWebRequest instance to send the multipart data. This is where you’ll add the proxy configuration.

  2. Apply system default proxy (matching your existing pattern)
    Use the same system proxy logic you already know, but apply it directly to the HttpWebRequest object:

    ' Inside your MultipartFormDataPost function
    Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
    request.Method = "POST"
    request.ContentType = "multipart/form-data; boundary=----YourCustomBoundary"
    
    ' Configure system proxy + default credentials
    request.Proxy = System.Net.WebRequest.GetSystemWebProxy()
    request.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials
    
  3. Optional: Use a custom proxy instead of system default
    If you need to target a specific proxy (not the system-wide one), instantiate a WebProxy directly:

    ' Custom proxy example with explicit credentials
    Dim customProxy As New System.Net.WebProxy("http://your-proxy-server:8080")
    customProxy.Credentials = New System.Net.NetworkCredential("proxy-username", "proxy-password")
    request.Proxy = customProxy
    
  4. Complete your multipart logic and get the response
    Finish building your multipart form data, write it to the request stream, then call GetResponse() to retrieve the HttpWebResponse—which will now route through your configured proxy.

Full Example Snippet

Here’s how the integrated code might look in your function:

Public Shared Function MultipartFormDataPost(url As String, postData As Dictionary(Of String, Object)) As HttpWebResponse
    ' Create the request
    Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
    request.Method = "POST"
    Dim boundary As String = "----MultipartBoundary" & DateTime.Now.Ticks.ToString("x")
    request.ContentType = $"multipart/form-data; boundary={boundary}"

    ' Configure proxy (system default with credentials)
    request.Proxy = System.Net.WebRequest.GetSystemWebProxy()
    request.Proxy.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials

    ' Build multipart form data (your existing logic here)
    Using stream As System.IO.Stream = request.GetRequestStream()
        For Each kvp In postData
            ' Write each form part to the stream...
            ' Your existing multipart writing code goes here
        Next
    End Using

    ' Send request and return response
    Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
    Return response
End Function

Key Notes

  • Always configure the proxy before calling GetRequestStream() or GetResponse()—once the request starts sending, you can’t modify proxy settings.
  • DefaultNetworkCredentials uses the current Windows user’s credentials, which works well for domain-managed proxies. For non-domain proxies, use NetworkCredential with explicit username/password.
  • If you want to bypass the proxy for specific addresses, you can use request.Proxy.BypassList to add exclusion rules.

内容的提问来源于stack exchange,提问作者Stefan Meyer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 10:08:39