2013-10-09 27 views
0

我想上傳一個字符串到服務器,但字符串中的+號不會被保存,而是被空間替換。因此,如果我發送ddd + fff作爲salt或iv,它將作爲ddd fff保存在sql數據庫中。爲什麼+符號迷路了?無法正確上傳字符串到服務器

感謝

在Android應用上傳代碼:

parameters ="id="+mUsername+"&pass="+mPassword+"&operationtype="+moperationtype+"&salt="+Base64.encode Bytes(salt)+"&iv="+Base64.encodeBytes(iv); 

      try 
      { 
       url = new URL("http://www.xyz.com/connect.php"); 
       connection = (HttpURLConnection) url.openConnection(); 
       connection.setDoOutput(true); 
       connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 
       connection.setRequestMethod("POST");  

       request = new OutputStreamWriter(connection.getOutputStream()); 
       request.write(parameters); 
       request.flush(); 
       request.close();    
       String line = "";    
       InputStreamReader isr = new InputStreamReader(connection.getInputStream()); 
       BufferedReader reader = new BufferedReader(isr); 
       StringBuilder sb = new StringBuilder(); 
       while ((line = reader.readLine()) != null) 
       { 
        sb.append(line + "\n"); 
       } 
       // Response from server after login process will be stored in response variable.     
       response = sb.toString(); 
       // You can perform UI operations here 
       Toast.makeText(this,"Message from Server: \n"+ response, 0).show(); 

       isr.close(); 
       reader.close(); 

      } 
      catch(Exception e) 
      { 

       // Error 
      } 

的connect.php的代碼是:

<?php 
$host="www.host.com"; // Host name 
$username="user"; // username 
$password="pass"; // password 
$db_name="db"; // Database name 
$tbl_name="table"; // Table name 
$usertable="table"; 

// Replace database connect functions depending on database you are using. 
mysql_connect($host, $username, $password) OR DIE ("Unable to 
     connect to database! Please try again later."); 

mysql_select_db("$db_name"); 

$operationtype="restore"; 
// username and password sent from form 
//NEVER Remove the mysql_real_escape_string. Else there could be an Sql-Injection! 
$myusername=mysql_real_escape_string($_POST['id']); 
$mypassword=mysql_real_escape_string($_POST['pass']); 
$salt=($_POST['salt']); 
$iv=($_POST['iv']); 
$operationtype=mysql_real_escape_string($_POST['operationtype']); 


$sql ="INSERT INTO `db`.`table` (`row`, `id`, `pass`,`salt`, `iv`) VALUES (NULL, '$myusername', '$mypassword','$salt','$iv');"; 

$result=mysql_query($sql); 


?> 
+0

是encodeURI夥計! – Nezam

回答

0

使用URLEncoder爲您的網址,讓所有的字符編碼得到正確的方式。不允許在URL中寫入特定字符,例如u'+ $§'。 並使用decode在你的PHP

URL規範:

Reserved 
Have to be encoded sometimes 
! * ' () ; : @ & = + $ ,/? % # [ ] 
+1

@ A.S.decoding在服務器端不是必需的。因爲他使用'Content-Type:application/x-www-form-urlencoded',php應該處理$ _POST預定義變量內的解碼。 – linakis

+0

哦,謝謝你! –

0

必須在獲取使用%2B爲PHP即

example.com?q=string%2Bblah 

EQ

string+blah 
相關問題