WebClient Error: The underlying connection was closed in Windows Application

Last Reply 8 months ago By dharmendr

Posted 8 months ago

I am trying to send sms using nexmo API in windows forms application

when I tried send it says "The underlying connection was closed: An unexpected error occurred on a send."

Here is my API info

curl -X "POST" "https://rest.nexmo.com/sms/json" \
     -d "from=Nexmo" \
     -d "text=Hello from Nexmo" \
     -d "to=********" \
     -d "api_key=18c03652" \
     -d "api_secret=****************"
 Dim SmsStatusMsg As String = String.Empty
       Try
            Dim client As WebClient = New WebClient()
            Dim url As String = "https://rest.nexmo.com/sms/xml?api_key=18c03652&api_secret=7Y3hqMnTF4EuE2HE&from=UCPH&to=9607908122&text=D%c3%a9j%c3%a0+vu"
            SmsStatusMsg = client.DownloadString(url)

            'MessageBox.Show("Status: " & client.SelectSingleNode("/response/status/status_msg").InnerText)
        Catch ex As Exception
            MessageBox.Show(ex.Message, ex.StackTrace)
        End Try

 

You are viewing reply posted by: dharmendr 8 months ago.
Posted 8 months ago Modified on 8 months ago

Hi satabeach,

'Add this three lines of code.

C#

ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
ServicePointManager.DefaultConnectionLimit = 9999;

VB.Net

ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)
ServicePointManager.DefaultConnectionLimit = 9999

Refer below modified code.

Dim SmsStatusMsg As String = String.Empty
Try
    Dim client As WebClient = New WebClient()
    Dim url As String = "https://rest.nexmo.com/sms/xml?api_key=18c03652&api_secret=7Y3hqMnTF4EuE2HE&from=UCPH&to=9607908122&text=D%c3%a9j%c3%a0+vu"
    
    ServicePointManager.Expect100Continue = True
    ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)  'its same like SecurityProtocolType.Tls12 
    ServicePointManager.DefaultConnectionLimit = 9999
    
    SmsStatusMsg = client.DownloadString(url)

    'MessageBox.Show("Status: " & client.SelectSingleNode("/response/status/status_msg").InnerText)
Catch ex As Exception
    Response.Write(ex.Message + " " + ex.StackTrace)
End Try