runtime icon indicating copy to clipboard operation
runtime copied to clipboard

GetResponseCallback() does not translate WebException into HttpResponseMessage in GetResponseCallback()

Open baal2000 opened this issue 8 years ago • 4 comments

Issue RE: HttpClientHandler.Net46.cs GetResponseCallback():

For unsuccessful http requests (for example: '404' Not Found) HttpClientHandler.Net46 implementation behaves differently than other implementations of HttpClientHandler. While HttpResponseMessage result is expected from await HttpClientHandler.SendAsync, the caller receives platform-specific WebException.

Proposed solution

                WebResponse webResponse;
                try
                {
                    // Try getting response.
                    webResponse = state.webRequest.EndGetResponse(ar);
                }
                catch (WebException webException)
                {
                    // Intercept WebException.
                    // Use webException.Response value (when available) to create HttpResponseMessage. 
                    webResponse = webException.Response;
                    if (webResponse == null)
                    {
                        throw;
                    }
                }
                state.tcs.TrySetResult(CreateResponseMessage((HttpWebResponse)webResponse, state.requestMessage));

[EDIT] Add C# syntax highlighting by @karelz

baal2000 avatar Jan 27 '18 16:01 baal2000

Do you have a small standalone repro of the behavior you are talking about? Or did you discover this via code inspection?

Originally, HttpClientHandler.Net46.cs was a copy of the actual .NET Framework source code. So, it is surprising that you are observing this behavior.

The code you reference is part of the .NET Framework support package in the release/2.0.0 branch which is no longer needed when you target NETStandard2.0 and run on top of .NET Framework 4.7.1 and later.

This particular code file, HttpClientHandler.Net46.cs is no longer built in the master branch. That is because we no longer build an OOB (out-of-band) copy of System.Net.Http.dll for .NET Framework. Instead, we always will rebind back to the inbox (GAC'd) version of .NET Framework.

Please see this issue for history of the original problem: https://github.com/dotnet/corefx/issues/17522

One workaround for this is to make sure you use binding redirects and redirect back to the original System.Net.Http.dll binary, 4.0.0.0. Please see dotnet/runtime#20777 for info on how to modify your project to do that.

davidsh avatar Jan 27 '18 17:01 davidsh

cc: @karelz

davidsh avatar Jan 27 '18 17:01 davidsh

@davidsh I have been running tests of a 3-rd party library that converts unsuccessful SendAsync responses into own exceptions.

baal2000 avatar Jan 27 '18 18:01 baal2000

No repro, closing. If you have a minimal repro, we can verify against .NET Framework and reopen.

karelz avatar May 17 '18 00:05 karelz