A problem has been encountered in the. NET core project recently, with an error following an interface that accesses HTTPS via httpclient:
WINHTTPEXCEPTION:A Security error occurred System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () system.ru Ntime. CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Task Task) System.runtime.compilerservices.configuredtaskawaitable+configuredtaskawaiter.getresult () System.net.http.winhttphandler+<startrequest>d__105.movenext () Httprequestexception:an error occurred while Sending the request. System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Task Task) System.runtime.compilerservices.configuredtaskawaitable+configuredtaskawaiter.getresult () System.net.http.httpclient+<finishsendasync>d__58.movenext () System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Task Task) System.Runtime.CompilerServices.TaskAwaiter.GetResult () myapp.weB.controllers.homecontroller.test () in HomeController.cs var response = client. Getasync ("Https://someurl.com/api.php?arg1=some&arg2=test"). Getawaiter (). GetResult (); Lambda_method (Closure, Object, object[]) microsoft.aspnetcore.mvc.internal.controlleractioninvoker+< Invokeactionmethodasync>d__27.movenext ()
By troubleshooting the discovery is. NETFramework 4.6 includes a new security feature that blocks unsafe passwords and hashing algorithms for connections.
This can be handled by the following solution:
using(varHandler =NewHttpclienthandler ()) {handler. Servercertificatecustomvalidationcallback= (sender, certificate, chain, sslpolicyerrors) =true; Handler. Sslprotocols= SSLPROTOCOLS.TLS12 | SSLPROTOCOLS.TLS11 |Sslprotocols.tls; using(HttpClient client =NewHttpClient (handler)) { stringRequestobjjson =Requestobj.tojson (); varAddress =NewUri ($"https://yourcompany.com/"); stringtoken =GetToken (); Client. BaseAddress=address; Client. DefaultRequestHeaders.Accept.Clear (); Client. DEFAULTREQUESTHEADERS.ACCEPT.ADD (NewMediatypewithqualityheadervalue ("Application/json")); Client. Defaultrequestheaders.authorization=NewAuthenticationheadervalue ("Bearer", token); varContentdata =NewStringcontent (Requestobjjson, System.Text.Encoding.UTF8,"Application/json"); using(varResponse =awaitClient. Postasync ("Yourcompany/api", Contentdata)) { varContent =Response. Content.readasstringasync (); varTaskresult =content. Result; Jobject resultobj=Jobject.parse (Taskresult); returnResultobj; } }}
It is important to note that for. NET Core 2.0, you need to use Httpclienthandler instead of ServicePointManager.
. NET core 2.0 HTTPS request fails using HttpClient security error