0

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

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

4

2 回答 2

0

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

于 2013-03-19T05:34:59.173 回答
-1

听起来您正在返回 Web 服务函数的结果,并让 .NET 处理所有底层 SOAP 细节。如果您想在代码中看到 HTTP SOAP 响应,您需要做的不是引用 Web 服务并调用函数,而是发出 HTTP SOAP 请求。在 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 响应。

于 2012-12-03T17:44:41.500 回答