Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't send xml to webservice - The underlying connection was closed. An unexpected error occurred on a send

I'm creating an application to submit some XML to a web-service. The issue is that I can't communicate with the web-service. I'm getting this error, when I call "request.GetRequestStream()":

The underlying connection was closed. An unexpected error occurred on a send.

ex.Status = SendFailure {4}

This is the code that I'm using:

Imports System.CodeDom.Compiler
Imports System.CodeDom
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Globalization
Imports System.IO
Imports System.Reflection
Imports System.Web.Services.Protocols
Imports System.Net.WebRequest
Imports System.Net
Imports System.Xml.Serialization
Imports System.Xml
Imports System.Uri
Imports System.Text
Imports System.Security.Policy
Imports System.Security
Imports System.Security.Cryptography
Imports System.Security.Cryptography.X509Certificates
Imports System.Security.Cryptography.SHA1CryptoServiceProvider
Imports System.Web.UI.Page
Imports System.Web.Services
Imports System.Windows.Forms.Application
Imports System.Data.OleDb
Imports Microsoft.VisualBasic.Logging



Private Function Send(oRequest As String) As String
         Dim CaminhoCertificado As String = StartupPath + "\certificados\TesteWebServices.pfx"
         Dim SenhaCertificado As String = "*********"

         Dim EnderecoWebService As String = "https://servicos.portaldasfinancas.gov.pt:709/ws/arrendamento"""
         Dim SoapAction As String = "https://servicos.portaldasfinancas.gov.pt/arrendamento/definitions/Arrendamento/registarDadosContratoRequest"


       Try

        Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(EnderecoWebService), HttpWebRequest)
        Dim cert As New X509Certificate2()
        cert.Import(CaminhoCertificado, SenhaCertificado, X509KeyStorageFlags.DefaultKeySet)



        request.ClientCertificates.Add(cert)

        ''''''''''''''''''''''''''''''''''''''''
        'System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3

        request.ProtocolVersion = HttpVersion.Version10
        request.AllowAutoRedirect = True
        request.UserAgent = "Mozilla/3.0 (compatible; My Browser/1.0)"
        'request.Proxy = System.Net.WebProxy.GetDefaultProxy()
        'request.UseDefaultCredentials = True
        'request.Credentials = CredentialCache.DefaultCredentials
        ''''''''''''''''''''''''''''''''''''''''

        request.Method = "POST"
        request.ContentType = "text/xml; charset=utf-8"
        request.Accept = "text/xml"
        request.KeepAlive = False


        request.Headers.Add("SOAPAction", SoapAction)
        Dim postData As String = oRequest
        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
        request.ContentLength = byteArray.Length
''''''''''''''''
            Dim dataStream As Stream = request.GetRequestStream() ''''error is triggered in this line
''''''''''''''''
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()
        Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        dataStream = response.GetResponseStream()

        Dim reader As New StreamReader(dataStream, Encoding.GetEncoding("windows-1252"))
        Dim responseFromServer As String = reader.ReadToEnd()

        reader.Close()
        dataStream.Close()
        response.Close()
        Return responseFromServer





    Catch ex As WebException
        My.Application.Log.WriteEntry("Error: " & ex.Message & " - " & ex.Status)
        If ex.Status = WebExceptionStatus.ProtocolError Then
            Dim resp As WebResponse = ex.Response
            Dim sr As New StreamReader(resp.GetResponseStream())
            Return sr.ReadToEnd()
        Else
            Return ex.Message & " - " & ex.Status
        End If

    End Try

End Function

The target framework is .Net Framework 4.5. How can I properly send the xml to this webservice?

like image 300
RSilva Avatar asked Nov 10 '22 01:11

RSilva


1 Answers

You commented out the ServicePointManager.SecurityProtocol assignment - it is probably required as the connection is HTTPS - This may be a duplicate of this.

like image 110
Sam Makin Avatar answered Nov 14 '22 23:11

Sam Makin