【问题标题】:passing a utf8 encoded string to web service将 utf8 编码的字符串传递给 Web 服务
【发布时间】:2013-06-11 18:33:07
【问题描述】:

我可以将 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仍然是编码的。

【问题讨论】:

  • 因为你还没有解码。

标签: asp.net vb.net web-services


【解决方案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)

希望这能解决您的问题。

【讨论】:

  • 好的,那么我想我需要在 Web 服务器上对 UrlDecode 做一些类似的事情?
  • 没用。我将很快用服务器端解码更新 OP。更新的操作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-09
  • 1970-01-01
  • 2018-04-12
  • 2012-04-10
  • 1970-01-01
  • 2016-05-29
  • 2023-03-26
相关资源
最近更新 更多