简体   繁体   English

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

[英]Problem encoding string to ISO8859-1

I'm using this code to convert string to ISO8859-1 我正在使用此代码将字符串转换为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 价格80+增值税

encoded string in my app and this string is sent to provider: 我的应用程序中的编码字符串,此字符串发送给提供程序:

price+80%2bVAT 价格+ 80%2bVAT

sms received in the mobile: 手机收到的短信:

price 80 VAT 价格80增值税


EDIT 编辑

Ineed to pass to URL some variables. 将一些变量传递给URL。 Because I have a program to send sms, And I need to send variables to URL (URL provider SMS system). 因为我有一个发送短信的程序,而且我需要将变量发送到URL(URL提供程序SMS系统)。 The string ( message mobile that the user writes in my program) must be sent encoded (ISO 8859-1). 字符串(用户在我的程序中编写的移动消息 )必须以编码方式(ISO 8859-1)发送。

For example, this code in PHP runs fine: 例如,PHP中的以下代码运行良好:

$texto=urlencode($textoOriginal);

This code returns this string converted: 此代码返回转换后的字符串:

price+80%2BVAT 价格+ 80%2BVAT


EDIT 2 编辑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? 因为如果我发送相同的字符串编码为“ price + 80%2BVAT”,为什么在VB.NET中代码无法运行,而在PHP中运行良好? Is the same encoded string. 是相同的编码字符串。

I see that you're passing the string to the querystring, so this is the method that you want to use. 我看到您正在将字符串传递给querystring,所以这是您要使用的方法。

What you need to do when you request the querystring back is use System.Web.HttpUtility.UrlDecode 当您请求返回查询字符串时,您需要做的是使用System.Web.HttpUtility.UrlDecode

Basically you do the following 基本上你要做以下

  1. UrlEncode the original text 用Url编码原始文本
  2. Pass it to the QueryString 将其传递给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. 原始字符串:这是带+号的My STRING。
Encoded String: This+is+My+STRING+with+a+%2B+symbol. 编码的字符串:这是+++ a +%2B +符号。
Decoded String: This is My STRING with a + symbol. 解码字符串:这是我的STRING,带有+符号。

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"))

That's part of URL encoding - + means a space in a URL. 这是URL编码的一部分- +表示URL中的空格。

In other words, this is correct behaviour if you actually want URL encoding. 换句话说,如果您确实需要URL编码,则这是正确的行为。 If you don't, please explain exactly what you're trying to do. 如果您不这样做,请确切说明您要做什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM