{“远程服务器返回错误:(500)内部服务器错误。”}调用Web服务时

时间:2014-02-04 02:51:58

标签: web-services soap

我正在尝试在运行时绑定到Web服务并调用一个简单的方法来使此代码正常工作,并在我尝试从Web服务获取回复时收到上述错误消息。下面是以下代码,首先是我如何调用服务:

    Public Sub BeginInvoke(invokeCompleted As AsyncCallback)
    Dim invoke As New DelegateInvokeService(AddressOf Me.InvokeWebService)

    Dim result As IAsyncResult = invoke.BeginInvoke(invokeCompleted, Nothing)
End Sub

Public Function EndInvoke(result As IAsyncResult) As String
    Dim asyncResult = DirectCast(result, AsyncResult)
    Dim message As ReturnMessage = DirectCast(asyncResult.GetReplyMessage(), ReturnMessage)

    Return message.ReturnValue.ToString()
End Function

Public Function InvokeWebService() As String
    Try
        'Create the request
        Dim request As HttpWebRequest = CreateWebRequest()

        'write the soap envelope to request stream
        Using s As Stream = request.GetRequestStream()
            Using writer As New StreamWriter(s)
                writer.Write(CreateSoapEnvelope())
            End Using
        End Using

        'get the response from the web service
        Dim response As WebResponse = request.GetResponse()

        Dim stream As Stream = response.GetResponseStream()

        Dim reader As New StreamReader(stream)

        Dim str = reader.ReadToEnd()

        Return StripResponse(HttpUtility.HtmlDecode(str))
    Catch ex As Exception
        Return ex.Message
    End Try
End Function

现在构建soap包和web请求:

Private Function CreateSoapEnvelope() As String
    Dim method As String = "<" & WebMethod & " xmlns=""http://tempuri.org/"">"
    Dim params As String = Parameters.Aggregate(String.Empty, Function(current, param) current & "<" & param.Name & ">" & param.Value & "</" & param.Name & ">")

    method &= params & "</" & WebMethod & ">"

    Dim sb As New StringBuilder(SoapEnvelope)
    sb.Insert(sb.ToString().IndexOf("</soap:Body>"), method)

    Return sb.ToString()
End Function

Private Function CreateWebRequest() As HttpWebRequest
    Dim request As HttpWebRequest = DirectCast(WebRequest.Create(Url), HttpWebRequest)
    If WSServiceType = ServiceType.WCF Then
        request.Headers.Add("SOAPAction", """http://tempuri.org/" & WCFContractName & "/" & WebMethod & """")
    Else
        request.Headers.Add("SOAPAction", """http://tempuri.org/" & WebMethod & """")
    End If

    request.Headers.Add("To", Url)

    request.ContentType = "text/xml;charset=""utf-8"""
    request.Accept = "text/xml"
    request.Method = "POST"
    Return request
End Function

SoapEnvelope 变量如下所示:

Private Const SoapEnvelope As String = "<soap:Envelope " & _
    "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' " & _
    "xmlns:xsd='http://www.w3.org/2001/XMLSchema' " & _
    "xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" & _
    "<soap:Body></soap:Body></soap:Envelope>"

这就是我调用上述调用的方式:

Protected Sub GoButtonClick(sender As Object, e As EventArgs) Handles GoButton.Click
    _oClient = New SoapClient()

    Dim params As New List(Of SoapClient.Parameter)
    params.Add(New SoapClient.Parameter() With {.Name = "str", .Value = "Richard"})

    With _oClient
        .Url = "http://localhost:7659/WebServices/SampleService.asmx"
        .WebMethod = "HelloWorld"
        .WSServiceType = SoapClient.ServiceType.Traditional
        .Parameters = params
    End With

    _oClient.BeginInvoke(AddressOf InvokeCompleted)
End Sub

Public Sub InvokeCompleted(result As IAsyncResult)
    ErrorMessage.Text = _oClient.EndInvoke(result)
End Sub

InvokeWebService

中的此行生成错误
'get the response from the web service
Dim response As WebResponse = request.GetResponse()

任何人都知道我在哪里错了吗?

1 个答案:

答案 0 :(得分:0)

事实证明,我正在测试的示例Web服务存在问题,一旦修复了该错误,上述代码就能很好地运行。