[Rest API] Get Error The underlying connection was closed: An unexpected error occur

7,527 views
Skip to first unread message

Tung Do

unread,
Jun 2, 2016, 12:51:54 AM6/2/16
to DevTargetProcess
Hi all,
At first my apologize if this question is existed somewhere else.

Today I got a problems when use third party library to send request (the HttpClient from .Net Framework)
- for Authentication I use Basic Authentication (username/password)

May I ask why? 

Many Thanks!

Stack Trace:
System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. 
---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. 
---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
   at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   --- End of inner exception stack trace ---
   at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Net.TlsStream.CallProcessAuthentication(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
   at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.ConnectStream.WriteHeaders(Boolean async)
   --- End of inner exception stack trace ---
   at System.Net.HttpWebRequest.GetResponse()

Alex Fomin

unread,
Jun 2, 2016, 2:11:03 AM6/2/16
to DevTargetProcess
Could you plz provide the full example of your code?

Thanks,
Alex, targetprocess team

Tung Do

unread,
Jun 2, 2016, 3:21:36 AM6/2/16
to DevTargetProcess
Thanks for your replied.

- Just a simple block of code like this
public static bool CheckForInternetConnection()
       
{
           
try
           
{
               
using (var client = new WebClient())
               
{
                   
using (var stream = client.OpenRead("https://globeteam.tpondemand.com"))
                   
{
                       
return true;
                   
}
               
}
           
}
           
catch(Exception ex)
           
{
               
return false;
           
}
       
}

Tung Do

unread,
Jun 2, 2016, 3:31:09 AM6/2/16
to DevTargetProcess
I just tried the https://md5.tpondemand.com/ and it request/response successful but https://globeteam.tpondemand.com (our company domain) it can not


On Thursday, June 2, 2016 at 1:11:03 PM UTC+7, Alex Fomin wrote:

Alex Fomin

unread,
Jun 2, 2016, 4:36:18 AM6/2/16
to DevTargetProcess

This happens because of enforcing TLS 1.2 on Targetprocess side. You can find more info here:


As for your code you need to explicitly enable TLS 1.2 support in your app using the following snippet:

System.Net.ServicePointManager.SecurityProtocol = System.Net.ServicePointManager.SecurityProtocol | System.Net.SecurityProtocolType.Tls12;

Thanks,
Alex, TP Team

Tung Do

unread,
Jun 2, 2016, 5:02:35 AM6/2/16
to devtarge...@googlegroups.com
ha... Its worked :D
You just saved my day
Thanks you very much!!!

--
You received this message because you are subscribed to a topic in the Google Groups "DevTargetProcess" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/devtargetprocess/1R1niz_WCFE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to devtargetproce...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Muhammad Salman Abdul Wahab

unread,
Jul 28, 2017, 10:14:39 AM7/28/17
to DevTargetProcess
where to add this line
System.Net.ServicePointManager.SecurityProtocol = System.Net.ServicePointManager.SecurityProtocol | System.Net.SecurityProtocolType.Tls12;

tsar...@targetprocess.com

unread,
Jul 28, 2017, 10:55:15 AM7/28/17
to DevTargetProcess
Hi! 

System.Net.ServicePointManager.SecurityProtocol = System.Net.ServicePointManager.SecurityProtocol | System.Net.SecurityProtocolType.Tls12;

code line should be added to your C# solution before any call to Targetprocess REST API.

You can see the examples here:

Reply all
Reply to author
Forward
0 new messages