如何为HttpWebResponse添加代理?附WebRequest代理配置示例
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
Locate the
HttpWebRequestcreation in your function
YourMultipartFormDataPostmethod must be creating anHttpWebRequestinstance to send the multipart data. This is where you’ll add the proxy configuration.Apply system default proxy (matching your existing pattern)
Use the same system proxy logic you already know, but apply it directly to theHttpWebRequestobject:' 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.DefaultNetworkCredentialsOptional: Use a custom proxy instead of system default
If you need to target a specific proxy (not the system-wide one), instantiate aWebProxydirectly:' 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 = customProxyComplete your multipart logic and get the response
Finish building your multipart form data, write it to the request stream, then callGetResponse()to retrieve theHttpWebResponse—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()orGetResponse()—once the request starts sending, you can’t modify proxy settings. DefaultNetworkCredentialsuses the current Windows user’s credentials, which works well for domain-managed proxies. For non-domain proxies, useNetworkCredentialwith explicit username/password.- If you want to bypass the proxy for specific addresses, you can use
request.Proxy.BypassListto add exclusion rules.
内容的提问来源于stack exchange,提问作者Stefan Meyer

