I’m trying to send post request to web service which have an email and password. When I add special character @ in parameter (i.e qadir.suh@gmail.com) it is converted to %40. I have checked server side, they are getting %40 instead of @.
Here is my code:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://www.myurl.com/requesthandler.ashx");
// from list to arraylist
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
3);
nameValuePairs.add(new BasicNameValuePair("txtUserId",
"qadir.suh@gmail.com"));
nameValuePairs.add(new BasicNameValuePair("txtPassword",
"blahblah"));
nameValuePairs.add(new BasicNameValuePair("type", "login"));
try {
httppost.setEntity(new UrlEncodedFormEntity(
nameValuePairs));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Execute HTTP Post Request
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("Client Protocol Excption", e + "");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("IO Exception", e + "");
}
String responseText = null;
try {
responseText = EntityUtils.toString(response
.getEntity());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("Parse Exception", e + "");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("IO Exception 2", e + "");
}
String afterDecode = null;
try {
afterDecode = URLDecoder.decode(responseText, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(MainActivity.this, afterDecode,
Toast.LENGTH_LONG).show();
I know that
httppost.setEntity(new UrlEncodedFormEntity(
nameValuePairs));
This encodes the URL in UTF-8. So how can I achieve my goal so that the server should receive the @ symbol instead of the %40? Or is there any method to POST the request without using setEntity(new UrlEncodedFormEntity(nameValuePairs))? Or is there any method so that we can send the decoded version of POST request?
You are posting URL-encoded form parameters, but probably not setting the content-type to “x-www-form-urlencoded”. This is needed so that the server will know how to interpret the posted parameters. Try adding this line before calling
httppost.setEntity():EDIT: If you don’t want to use the URL-encoding, you can simply post a string
If you don’t want to URL-encode the POST body, you can do this instead: