Search code examples
c#angularjserror-handlingumbraco

HTTP request with web client. Random error with (Verification failed because the other party has closed the transport stream.)


Hey hello i am working atm with MVC with Umbraco and i am trying to make API call with webclient http protocol.

This is the request i am doing.

[HttpPost]
public JObject CheckAuth()
{

    string URI = "https://nedfinity.api.accelo.com/oauth2/v0/token";
    string myParameters = "grant_type=client_credentials&scope=read(all)";

    var clientId = "this is a secret";
    var clientSecret = "this is a secret";

    var byteArray = new UTF8Encoding().GetBytes(clientId + ":" + clientSecret);

    using (WebClient wc = new WebClient())
    {
        wc.UseDefaultCredentials = true;
        wc.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
        wc.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(byteArray);
        string HtmlResult = wc.UploadString(URI, myParameters);
        JObject json = JObject.Parse(HtmlResult);
        return json;
    }
}

It used to work just fine all the time but out of no where when i was editiing CSS it stopped working. I tryed to reverse all my commits in bitbucket but no fix. Also when try to call the api from accelo its working correctly. This the only feedback i am getting from the server:

data
:
Object
ExceptionMessage
:
"Verification failed because the other party has closed the transport stream." 
ExceptionType
:
"System.IO.IOException"
Message
:
"An error has occurred."
StackTrace
:
"   bij System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest)
↵   bij System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
↵   bij System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
↵   bij System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
↵   bij System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
↵   bij System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
↵   bij System.Net.TlsStream.CallProcessAuthentication(Object state)
↵   bij System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
↵   bij System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
↵   bij System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
↵   bij System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
↵   bij System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
↵   bij System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
↵   bij System.Net.ConnectStream.WriteHeaders(Boolean async)"

Anyone knows what can cause this problem and how to fix it or where to look?

I am thinking my self it has to do with some config settings or network permissions but it is on a localhost and i didn't change anything in this time that could cause this.

P.S. I hope this correct way to ask a question if did something wrong feel free to point me on that.


Solution

  • Can you try setting the security protocol to the following:

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
    

    Most API's now use TLS12 as the default protocol.

    Let me know how it goes.

    PS. You can add this inside your existing method block.

    Regards

    Craig