gönderiminde beklenmeyen bir hata oluştu Bir web servisine bazı XML göndermek için bir uygulama oluşturuyorum. Sorun, web hizmeti ile iletişim kuramadığım.Web hizmetine xml gönderilemiyor - Temel bağlantı kapatıldı.
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
hedef çerçevesi .Net Framework 4.5 geçerli: Bu kullanıyorum kodudur
The underlying connection was closed. An unexpected error occurred on a send.
ex.Status = SendFailure {4}
: Ben "request.GetRequestStream()" demenden, bu hatayı alıyorum . Bu web servisine xml'yi nasıl düzgün gönderebilirim?
InnerException'da neler var? Ex.Message özelliği oldukça sınırlı olduğundan ex.ToString, her şeyi almak için oturum açmak daha iyidir. –
http://stackoverflow.com/a/28439582/2319909 –
Tamam, pfx dosyasını Windows'a yükledim ve şimdi "HttpWebResponse olarak" Dim cevabı "na işaret eden bir" sunucu hatası: 500 "aldım. Bu benim ilk saygımla ilgili iyi bir şey sanırım doğru mu? – RSilva