Password containg '+' symbol [closed]
In my app there is an issue regarding the password.The problem occurs when a password ends with a + symbol,and while posting the url i got the response as user not found.If any one know the solutions plz help..I think the problem is with encoding of + symbol.so how can i send a password contain + symbol to the server?This is my code for posting url.'
public void doPost (String email,String password) throws UnsupportedEncodingException, SprinklrHttpException{
try{
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
OutputStream ostream=connection.getOutputStream();
String serverMsg=null;
serverMsg="email="+email+"&password="+password;
Log.i("tag","servermsg="+serverMsg);
ostream.write(serverMsg.getBytes());
ostream.close();
InputStream istream=connection.getInputStream();
response=convertStreamToString(istream);
istream.close();
}catch(Exception e){
e.printStackTrace();
}
}
private void executeRequest(HttpUriRequest request) throws SprinklrHttpException{
HttpResponse httpResponse;
InputStream inputStream = null;
HttpClient httpClient = ne开发者_JAVA技巧w DefaultHttpClient();
httpClient.getParams().setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
try {
httpResponse = httpClient.execute(request, this.httpContext);
this.responseCode = httpResponse.getStatusLine().getStatusCode();
Log.i("webResponse","responsecode"+this.responseCode);
this.message = httpResponse.getStatusLine().getReasonPhrase();
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
inputStream = entity.getContent();
this.response = convertStreamToString(inputStream);
}
}catch (ClientProtocolException e) {
httpClient.getConnectionManager().shutdown();
e.printStackTrace();
}catch (IOException e) {
httpClient.getConnectionManager().shutdown();
e.printStackTrace();
throw new SprinklrHttpException(1000,"");
}finally {
if (inputStream != null){
try {
inputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
Your code is pretty much unreadable due to bad formatting (Something went wrong while posting it methinks), but you probably have to URL-encode it for the + to get read properly.
In fact, you should always URL-encode all parameters (individually) that you send in the URL of a request if there's any chance of it containing characters outside a-zA-Z0-9. Just apply the URLEncoder.encode() method to your password string, and you should be good to go.
Edit: Formatting got fixed, my assumption was right. You should set "password=" + URlEncode.encode(password), preferably with the right encoding as a second argument.
REPLACE
serverMsg="email="+email+"&password="+password;
WITH
serverMsg="email="+URLEncoder.encode(email, "utf-8")+"&password="+URLEncoder.encode(password, "utf-8");
精彩评论