views:

317

answers:

2

Hi

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.

+2  A: 

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.

Jon Skeet
i have edited my first message.
yae
+2  A: 

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"))
rockinthesixstring
I only have to send the text codified to the system of my SMS supplier.
yae
So are they not appropriately decrypting it? Maybe ask them how they want you to send a "+" symbol.
rockinthesixstring
This is a total long shot, but did you try sending the encode command without the ISO specifyer? `System.Web.HttpUtility.UrlEncode(smsMessage)`
rockinthesixstring
If I send it without specifying ISO,the message is received incorrectly. But if I specify the ISO 8859-1, the message is received fine except this character +
yae
The SMS provider only said to me that the message must be codified in ISO8859 1. For example, in PHP if you do this: "$mensaje=urlencode($mensaje);" it runs fine (It is not done not especially to codify the character +). But still I have not managed to codify in VB.NET
yae
You've got me stumped. Hopefully someone else can chime in.
rockinthesixstring
Yes. For weeks I am in the same problem and still I could not have solved it. so easy in PHP and that does not have solution for VB.NET :-(
yae
I've made a quick edit to my post.
rockinthesixstring
thank you for your reply. I tried but I have the same error. But I have detected this small difference. For example I encode this string "120+20" in PHP returns "120%2B20" and if I encode in VB.NET returns "120%2b20". The difference is "b". One is uppercase and other lowercase. Is this the problem?
yae
This is a hack, but can you try a String.Replace(encoded, "%2b", "%2B") and see what happens?
rockinthesixstring
I have edited my first edit where you can see part of my real code.
yae
See my edits above^^
rockinthesixstring
Yes, I have replaced it but not runs. Same problem. But my code is correct? I think that my code is not good. Because I have sent the same string encoded "price+80%2BVAT" why in VB.NET code not runs and in PHP runs fine? Is the same encoded string.
yae