2010-02-18 74 views
2

我使用此代碼字符串轉換爲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() 

但問題是,當用戶寫這個卡拉科特:+


實例:

用戶寫在我的應用程序:

價格80 +增值稅

在我的應用程序

編碼字符串,這個字符串被髮送到服務提供商:

價格+ 80%2bVAT在移動接收

短信:

價格80增值稅


編輯

需要傳遞給URL一些變量。因爲我有一個程序來發送短信,我需要發送變量到URL(URL提供商SMS系統)。字符串(消息移動,用戶在我的程序中寫入)必須發送編碼(ISO 8859-1)。

例如,該代碼在PHP細運行:

$texto=urlencode($textoOriginal); 

此代碼返回該字符串轉換:

價+ 80%2BVAT


編輯2

我認爲我的代碼是錯誤的。因爲如果我發送相同的字符串編碼「價格+ 80%2BVAT」,爲什麼在VB.NET代碼不運行,並在PHP運行良好?是相同的編碼字符串。

+1

那就是UrlEncode – 2010-02-18 15:38:33

回答

2

這是URL編碼的一部分 - +表示URL中的空格。

換句話說,這是正確的行爲,如果你實際上想要URL編碼。如果你不這樣做,請解釋你正在嘗試做什麼。

+0

的默認行爲我編輯了我的第一條消息。 – aco 2010-02-19 18:19:40

3

我看到你將字符串傳遞給查詢字符串,所以這是你想要使用的方法。

時需要您請求的查詢字符串的回覆是使用System.Web.HttpUtility.UrlDecode

基本上你以下

  1. 以UrlEncode原文做什麼
  2. 它傳遞給查詢字符串
  3. 請求的QueryString
  4. Url將其解碼以獲取原始文本。

這是我運行的一個測試,似乎工作。

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 

這是我的結果

原始字符串:這是我的一個+符號串。
編碼字符串:此+是+ My + STRING +帶+ 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")) 
+0

我只需要將編寫的文本發送到我的SMS供應商系統。 – aco 2010-02-18 19:00:40

+0

那麼他們是不是正確地解密它?也許問他們想讓你發送一個「+」符號。 – 2010-02-19 01:25:28

+0

這是一個遠射,但你是否嘗試發送編碼命令沒有ISO說明? 'System.Web.HttpUtility.UrlEncode(smsMessage)' – 2010-02-19 01:29:19