.NET Core 3.1控制台应用部署至IIS后调用API出现请求发送错误的问题求助
Hey there, let's work through this problem together. The core issue here is that your HttpClient is being blocked by proxy settings in the IIS server environment—even though you tried the web.config fix, .NET Core doesn't always respect that configuration for HttpClient calls. Here are the most straightforward solutions:
1. Force HttpClient to Disable Proxy Directly in Code
This is the quickest fix, mirroring what you did in Postman/browser by turning off proxy usage. Modify your HttpClient initialization to use an HttpClientHandler with UseProxy set to false:
public async Task<string> InsertUser() { try { UserModel user = new UserModel() { Id = Guid.NewGuid(), Name = "name1" }; // Create handler with proxy disabled var handler = new HttpClientHandler { UseProxy = false, // Optional: Add this only if you encounter SSL certificate issues (use cautiously) // ServerCertificateCustomValidationCallback = (sender, cert, chain, sslPolicyErrors) => true }; // Reuse HttpClient instance to avoid socket exhaustion issues using var client = new HttpClient(handler); client.BaseAddress = new Uri("http://10.70.2.190:5000/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.AcceptCharset.Add(new StringWithQualityHeaderValue("utf-8")); string body = JsonConvert.SerializeObject(user); // Fixed typo: was 'd' instead of 'user' string signature = GetRSA(body, privatekey); client.DefaultRequestHeaders.Add("signature", signature); HttpResponseMessage resPost = await client.PostAsJsonAsync("api/User", user); // Add response validation to catch further issues resPost.EnsureSuccessStatusCode(); return await resPost.Content.ReadAsStringAsync(); } catch (Exception ex) { Console.WriteLine($"Error: {ex.Message}\nInner Exception: {ex.InnerException?.Message}\nStack Trace: {ex.StackTrace}"); return null; } }
Quick notes: I fixed a small typo in your code (JsonConvert.SerializeObject(d) → JsonConvert.SerializeObject(user)) and added response validation—this will help catch any additional issues once the connection is working. Also, reusing HttpClient instances is critical to avoid socket exhaustion in long-running applications.
2. Bypass Proxy for the Target API (If You Need to Keep Proxy Enabled for Other Calls)
If your app needs to use a proxy for other requests but bypass it for this specific API, configure the handler to exclude the target IP:
var handler = new HttpClientHandler { UseProxy = true, Proxy = new WebProxy() { // Bypass proxy for the target API's IP/domain BypassList = new[] { "10.70.2.190" } } };
3. Verify IIS Application Pool Permissions
Even if Postman can reach the API, the IIS application pool's identity might not have the same network access. Check:
- Is the application pool using an identity with outbound network permissions? (e.g.,
ApplicationPoolIdentitymight have restricted access—try switching toLocalSystemtemporarily to test) - Is the server's firewall blocking outbound traffic to port 5000 on
10.70.2.190?
Why Your web.config Fix Didn't Work
.NET Core has limited support for the <system.net><defaultProxy> section in web.config, especially for console apps hosted in IIS. The framework prioritizes code-based configuration or settings from appsettings.json over these legacy web.config nodes.
内容的提问来源于stack exchange,提问作者ariefs

