Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have REST API installed at Azure cloud .

it gives response from browser and also working well with Postman but does not give response from my vb.net application.

it just gives exception "The underlying connection was closed: An unexpected error occurred on a send."

more over the same API is working on other web server but does not work on Azure cloud

my codes as as under,

Dim URL As String

       URL = txtBookingURL.Text & "/api/values"


       Dim objWebRequest As Net.HttpWebRequest

       Dim objWebResponse As Net.HttpWebResponse

       Dim srReader As IO.StreamReader

       Dim strHTML As String

       objWebRequest = CType(Net.WebRequest.Create(URL), Net.HttpWebRequest)

       objWebRequest.Method = "GET"

       objWebRequest.ContentType = "application/json"

       objWebRequest.Credentials = CredentialCache.DefaultCredentials

        objWebRequest.Accept = "application/json"


       Dim response As HttpWebResponse

Try
           response = objWebRequest.GetResponse()     ' gives exception on this line

           objWebResponse = CType(response, Net.HttpWebResponse)
           srReader = New IO.StreamReader(objWebResponse.GetResponseStream)

           strHTML = srReader.ReadToEnd

           objWebResponse.Close()
           objWebRequest.Abort()


           txtReceived.Text = strHTML


       Catch ex As Exception

            txtReceived.Text = ex.Message

       End Try


What I have tried:

i tried using webclient and restsharp also but same error
Posted
Updated 24-Apr-20 4:20am
Comments
Richard Deeming 24-Apr-20 6:37am    
Debug your code and examine the exception. There is probably an inner exception with more details about the actual error.

If not, configure network tracing for your application and examine the output for clues:
How to: Configure Network Tracing | Microsoft Docs[^]
Member 3020851 24-Apr-20 9:32am    
following is detailed exception

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()
at testCornAPI.Form1.Button1_Click(Object sender, EventArgs e) in E:\mssource2018\mssource\CornAPI\testCornAPI\testCornAPI_200424\testCornAPI\Form1.vb:line 55

i tried by adding following and checked with all protocols TTL to ssl3

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3

but everytime same exception.

my codes are working if i put same API on another web server with https but it does not work on azure

following is full link
https://cornapi.azurewebsites.net/api/values


Please suggest if possible, thanks
Richard Deeming 24-Apr-20 9:41am    
Your code works fine for me, and returns:
["value1","value2"]

There must be something else getting in the way - perhaps there's a firewall involved?
Member 3020851 24-Apr-20 9:59am    
Thank you for your support sir,
i stopped the firewall and antivirus and internet security but still it is not working.

anyways, if it is working with you then it should work with me too. i will try to check some more options.

1 solution

I am able to do it now. i upgraded dot net framework of my application from 4.5.2 to 4.6.2 and it is working now.

Thank you all for your support
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900