无法将xml发送到webservice - 底层连接已关闭。发送时发生意外错误

时间:2015-11-17 10:29:48

标签: vb.net web-services .net-4.5

我正在创建一个应用程序来向Web服务提交一些XML。 问题是我无法与网络服务进行通信。当我调用“request.GetRequestStream()”:

时,我收到此错误
  

基础连接已关闭。发生意外错误   发送。

     

ex.Status = SendFailure {4}

这是我正在使用的代码:

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

目标框架是.Net Framework 4.5。 如何正确地将xml发送到此Web服务?

2 个答案:

答案 0 :(得分:1)

您注释了ServicePointManager.SecurityProtocol分配 - 可能需要它,因为连接是HTTPS - 这可能与this重复。

答案 1 :(得分:0)

这通常与对象上的序列化问题有关。确保由于要发布XML序列化的对象,所以定义它们没问题。 例如

[Serializable]
[XmlRoot(ElementName="Person")]
public class Person
{
     [XmlElement(ElementName="FirstName")]
     public string Name { get; set; }

     [XmlElement(ElementName="LastName")]
     public string LastName { get; set; }
}

确保将XmlRoot和Serializable添加到对象中,以便可以对其进行序列化和反序列化。