1

我可以将 UTF8 编码的字符串传递给 Web 服务吗?

更新:

Function EncodeToUTF(ByVal toEncode As String) As String
    Dim utf8 As New UTF8Encoding()

    Dim encodedBytes As Byte() = utf8.GetBytes(toEncode)
    Dim returnValue As String = utf8.GetString(encodedBytes)
    returnValue = HttpUtility.UrlEncode(returnValue)
    Return returnValue
End Function 

然后在网络服务器上解码?问题是 XML Web Service 解析器正在从我的字符串中删除 CR。

然后在服务器端解码:

Function DecodeFromUTF8(ByVal encodedData As String) As String
    Dim utf8 As New UTF8Encoding()

    Dim returnValue As String = HttpUtility.UrlDecode(encodedData)
    Dim encodedDataAsBytes As Byte() = utf8.GetBytes(returnValue)

    returnValue = utf8.GetString(encodedDataAsBytes)

    Return returnValue
End Function

这里的 returnValue 仍然是编码的。

4

1 回答 1

1

您需要对您的字符串/消息进行 URL 编码。消息在发送到 Web 服务之前必须经过 URL 编码,因为它们包含特殊字符。

Function EncodeToUTF(ByVal toEncode As String) As String
    Dim utf8 As New UTF8Encoding()

    Dim encodedBytes As Byte() = utf8.GetBytes(toEncode)
    Dim returnValue As String = utf8.GetString(encodedBytes)

    '' URL encode your string before pushing it to a web service
    returnValue = HttpUtility.UrlEncode(returnValue)

    Return returnValue
End Function

并且,在另一服务器端,使用以下代码来解码您的编码字符串:

Dim decodedUrl As String = HttpUtility.UrlDecode(encodedUrl)

希望这能解决您的问题。

于 2013-06-11T18:40:11.023 回答