在asp.net上获取SOAP中的Web服务响应

时间:2012-09-30 07:18:03

标签: asp.net soap web

我已经设置了一个带有.asmx文件的Web服务,并且它的Web方法是通过客户端上的Ajax(所有使用asp.net scriptmanager等)调用的。

当我调用webservice并查看回调中返回值的值时,它永远不会采用'SOAP'格式,即在xml中。相反,该值以原始形式返回。 因此,例如,如果我从webservice返回一个字符串,则传递给我成功回调的结果是字符串,不是由XML标记编码或包围的。 我怎样才能改变这一点,以便以SOAP格式看到它?

2 个答案:

答案 0 :(得分:0)

你是从jquery打来的吗?可能以Json格式返回。我的猜测是没有看到你的代码。

答案 1 :(得分:-1)

听起来您正在返回Web服务功能的结果,并让.NET处理所有底层SOAP详细信息。如果要在代码中查看HTTP SOAP响应,则需要执行的操作是发出HTTP SOAP请求,而不是引用Web Service并调用该函数。在VB.NET中:

Dim _soapRequest As String = "<?xml version=""1.0"" encoding=""utf-8""?>" & _
                             "<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>" & _
                                "<CelsiusToFahrenheit xmlns=""http://tempuri.org/"">" & _
                                "<Celsius>" & 100 & "</Celsius>" & _
                                 "</CelsiusToFahrenheit>" & _
                                "</soap:Body>" & _
                                "</soap:Envelope>"
Dim response As String = DoRequestResponse(_soapRequest, "http://localhost:88/Service1.asmx")

并且DoRequestResponse函数看起来像这样

Public Function DoRequestResponse(ByVal _p_RequestString As String, ByVal _p_RequestURL As String) As String

    Dim _httpWebRequest As HttpWebRequest
    Dim _httpWebResponse As HttpWebResponse
    Dim _streamReq As Stream
    Dim _streamResp As Stream
    Dim _streamReader As StreamReader
    Dim _responseString As String
    Dim _bytesToWrite() As Byte

    Try
        _httpWebRequest = CType(WebRequest.Create(_p_RequestURL), HttpWebRequest)
        _httpWebRequest.Method = "POST"
        _httpWebRequest.ContentType = "text/xml"
        _httpWebRequest.Timeout = 30000
        Dim EncodingType As System.Text.Encoding = System.Text.Encoding.UTF8
        _bytesToWrite = EncodingType.GetBytes(_p_RequestString)

        _streamReq = _httpWebRequest.GetRequestStream()
        _streamReq.Write(_bytesToWrite, 0, _bytesToWrite.Length)
        _streamReq.Close()

        _httpWebResponse = DirectCast(_httpWebRequest.GetResponse(), HttpWebResponse)
        _streamResp = _httpWebResponse.GetResponseStream()

        _streamReader = New StreamReader(_streamResp)
        _responseString = _streamReader.ReadToEnd()

        _streamReader.Close()
        _httpWebResponse.Close()

    Catch ex As Exception
        Dim _ex As WebException = ex
        Console.Write(_ex.Status)
        Console.Write(DirectCast(_ex.Response, HttpWebResponse).StatusCode)
        Throw New Exception("DoRequestResponse Error :" & vbCrLf & ex.Message)
    End Try

    Return _responseString

End Function

您可以在asp.net页面的代码隐藏中执行此类操作,并通过回发等方式从AJAX调用它,然后将其发布到.asmx Web服务并返回SOAP响应。 / p>