Have ASP.Net core Web API which is deployed on server. When I access/call the Web API/endpoint through post man I am getting the result but not when calling through .Net code (HttpClient), instead getting the errors. However, The ASP.Net core Web API is just working fine when its running on local machine.
Errors
1 or 4 Error : HttpRequestException: An error occurred while sending the request.
2 or 4 Error : WebException: The underlying connection was closed: An unexpected error occurred on a send.
3 or 4 Error : IOException: Unable to read data from the transport connection: An existing 4 or 4 Error : connection was forcibly closed by the remote host.
Why it not working when deployed on server, but working through Postman or when API running locally ?
2
Answers
After setting up security protocol inside HttpCLient works for me.
HTTP Client Configuration: Compare the configuration of the HTTP client in your .NET Core application with the settings you’re using in Postman. There might be differences in headers, timeouts, or proxy settings that could affect the behavior of the requests. For instance, if you’re using a proxy in Postman but not configuring it in your .NET Core application, this could lead to connectivity issues.
TLS/SSL Version: Ensure that your .NET Core application supports the same TLS/SSL protocols as Postman. If the server requires a specific version of TLS and your application is not configured to use it, this could cause connection issues. You can specify the TLS version in your .NET Core application by setting the ServicePointManager.SecurityProtocol property.
Network Environment: If your application works in Postman but not from your development environment, consider whether there are any network differences affecting the connection. For example, if you’re behind a corporate firewall or VPN on your local machine that Postman requests are not subject to, this could impact connectivity.
Authentication and Authorization: Verify that any authentication or authorization headers you’re sending from Postman are also being correctly set in your .NET Core application’s requests. A missing or incorrect header could lead to the server closing the connection or refusing to process the request.
Error Handling and Logging: Enhance error handling and logging in your .NET Core application to capture more details about the failure. This can help identify whether the issue occurs during the connection phase, while sending the request, or upon receiving the response. Logging the exact request details can also help in comparing the request made by your application with the one made by Postman.
Proxy Issues: If your local environment requires a proxy to access the internet but Postman is configured to bypass the proxy (or vice versa), this could explain the discrepancy. Check the proxy settings in your .NET Core application and ensure they match the environment’s requirements.
Testing with a Simplified Request: Try simplifying the request in your .NET Core application to match exactly what works in Postman, including URL, headers, and body. This can help identify if a specific part of the request is causing the issue.