我可以将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仍然是编码的。
答案 0 :(得分: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)
希望这能解决您的问题。