将字符串编码为 ISO8859-1 时出现问题

发布于 2024-08-22 01:10:08 字数 2002 浏览 9 评论 0原文

我正在使用此代码将字符串转换为 ISO8859-1

baseurl = "http://myurl.com/mypage.php"
                client = New WebClient
                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
                client.QueryString.Add("usuario", user)
                client.QueryString.Add("password", pass)
                client.QueryString.Add("ruta", 2)
                client.QueryString.Add("remitente", Me.txtRemite.Text)
                If Me.chkPRefijo.Checked = True Then
                    client.QueryString.Add("destinatarios", Me.LookUpEdit1.EditValue & Me.dtsMvl.Tables(0).Rows(x).Item("movil"))
                Else
                    client.QueryString.Add("destinatarios", Me.dtsMvl.Tables(0).Rows(x).Item("movil"))
                End If
                textoSms = Me.mmTexto.Text
                textoSms = System.Web.HttpUtility.UrlEncode(textoSms, System.Text.Encoding.GetEncoding("ISO-8859-1"))
                client.QueryString.Add("mensaje", textoSms)
                client.QueryString.Add("reporte", 1)
                data = client.OpenRead(baseurl)
                reader = New StreamReader(data)
                s = reader.ReadToEnd()
                data.Close()
                reader.Close()

但问题是当用户写入此字符时:+


示例:

用户在我的应用程序中写入:

我的应用程序中的价格 80+VAT

编码字符串,该字符串发送给提供商:

价格+80%2bVAT

手机中收到的短信:

价格 80 增值税


< strong>编辑

需要向 URL 传递一些变量。因为我有一个发送短信的程序,并且我需要将变量发送到URL(URL提供者短信系统)。字符串(用户在我的程序中写入的message mobile)必须以编码方式发送(ISO 8859-1)。

例如,PHP 中的这段代码运行良好:

$texto=urlencode($textoOriginal);

此代码返回转换后的字符串:

price+80%2BVAT


EDIT 2

我认为我的代码是错误的。因为如果我发送相同的字符串编码“price+80%2BVAT”,为什么在 VB.NET 代码中无法运行而在 PHP 中运行良好?是相同的编码字符串。

I'm using this code to convert string to ISO8859-1

baseurl = "http://myurl.com/mypage.php"
                client = New WebClient
                client.Headers.Add("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)")
                client.QueryString.Add("usuario", user)
                client.QueryString.Add("password", pass)
                client.QueryString.Add("ruta", 2)
                client.QueryString.Add("remitente", Me.txtRemite.Text)
                If Me.chkPRefijo.Checked = True Then
                    client.QueryString.Add("destinatarios", Me.LookUpEdit1.EditValue & Me.dtsMvl.Tables(0).Rows(x).Item("movil"))
                Else
                    client.QueryString.Add("destinatarios", Me.dtsMvl.Tables(0).Rows(x).Item("movil"))
                End If
                textoSms = Me.mmTexto.Text
                textoSms = System.Web.HttpUtility.UrlEncode(textoSms, System.Text.Encoding.GetEncoding("ISO-8859-1"))
                client.QueryString.Add("mensaje", textoSms)
                client.QueryString.Add("reporte", 1)
                data = client.OpenRead(baseurl)
                reader = New StreamReader(data)
                s = reader.ReadToEnd()
                data.Close()
                reader.Close()

But the problem is when an user writes this caracter: +


EXAMPLE:

user writes in my app:

price 80+VAT

encoded string in my app and this string is sent to provider:

price+80%2bVAT

sms received in the mobile:

price 80 VAT


EDIT

Ineed to pass to URL some variables. Because I have a program to send sms, And I need to send variables to URL (URL provider SMS system). The string (message mobile that the user writes in my program) must be sent encoded (ISO 8859-1).

For example, this code in PHP runs fine:

$texto=urlencode($textoOriginal);

This code returns this string converted:

price+80%2BVAT


EDIT 2

I think that my code is wrong. Because if I send the same string encoded "price+80%2BVAT", Why in VB.NET code not runs and in PHP runs fine? Is the same encoded string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦忆晨望 2024-08-29 01:10:08

我看到您正在将字符串传递给查询字符串,因此这就是您要使用的方法。

当您请求返回查询字符串时,您需要做的是使用 System.Web.HttpUtility.UrlDecode

基本上,您执行以下

  1. UrlEncode 原始文本
  2. 将其传递给 QueryString
  3. 请求 QueryString
  4. UrlDecode 它以获取原始文本回短信。

这是我运行的一个测试,似乎有效。

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim str As String = "This is My STRING with a + symbol."
    Dim encoded As String = Server.UrlEncode(str)
    Dim decoded As String = Server.UrlDecode(encoded)

    Dim sb As New StringBuilder
    sb.Append("Original String: ")
    sb.Append(str)
    sb.Append("</br>")
    sb.Append("Encoded String: ")
    sb.Append(Replace(encoded, "%2b", "%2B"))
    sb.Append("</br>")
    sb.Append("Decoded String: ")
    sb.Append(decoded)

    Response.Write(sb.ToString)
End Sub

这是我的结果

原始字符串:这是带有 + 符号的我的字符串。
编码字符串:This+is+My+STRING+with+a+%2B+符号。
解码后的字符串:这是带有 + 符号的“我的字符串”。

编辑:
看到您的编辑后,请尝试下面的操作...

textoSms = Replace(System.Web.HttpUtility.UrlEncode(Me.mmTexto.Text, System.Text.Encoding.GetEncoding("ISO-8859-1")), "%2b", "%2B")

而不是使用您此处的内容...

    textoSms = Me.mmTexto.Text
    textoSms = System.Web.HttpUtility.UrlEncode(textoSms, System.Text.Encoding.GetEncoding("ISO-8859-1"))

I see that you're passing the string to the querystring, so this is the method that you want to use.

What you need to do when you request the querystring back is use System.Web.HttpUtility.UrlDecode

Basically you do the following

  1. UrlEncode the original text
  2. Pass it to the QueryString
  3. Request the QueryString
  4. UrlDecode it to get the original text back.

Here's a test that I ran that seemed to work.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim str As String = "This is My STRING with a + symbol."
    Dim encoded As String = Server.UrlEncode(str)
    Dim decoded As String = Server.UrlDecode(encoded)

    Dim sb As New StringBuilder
    sb.Append("Original String: ")
    sb.Append(str)
    sb.Append("</br>")
    sb.Append("Encoded String: ")
    sb.Append(Replace(encoded, "%2b", "%2B"))
    sb.Append("</br>")
    sb.Append("Decoded String: ")
    sb.Append(decoded)

    Response.Write(sb.ToString)
End Sub

And here's my results

Original String: This is My STRING with a + symbol.
Encoded String: This+is+My+STRING+with+a+%2B+symbol.
Decoded String: This is My STRING with a + symbol.

EDIT:
After seeing your edit, Try this bit below...

textoSms = Replace(System.Web.HttpUtility.UrlEncode(Me.mmTexto.Text, System.Text.Encoding.GetEncoding("ISO-8859-1")), "%2b", "%2B")

Instead of using what you have here...

    textoSms = Me.mmTexto.Text
    textoSms = System.Web.HttpUtility.UrlEncode(textoSms, System.Text.Encoding.GetEncoding("ISO-8859-1"))
心作怪 2024-08-29 01:10:08

这是 URL 编码的一部分 - + 表示 URL 中的空格。

换句话说,如果您实际上想要 URL 编码,那么这是正确的行为。如果您不这样做,请准确解释您想要做什么。

That's part of URL encoding - + means a space in a URL.

In other words, this is correct behaviour if you actually want URL encoding. If you don't, please explain exactly what you're trying to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文