Problem encoding string to ISO8859-1
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)
开发者_StackOverflow中文版 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.
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
- UrlEncode the original text
- Pass it to the QueryString
- Request the QueryString
- 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"))
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.
精彩评论