密码包含'+'符号

时间:2011-07-26 08:35:11

标签: android

在我的应用程序中存在关于密码的问题。当密码以+符号结尾时发生问题,并且在发布网址时我得到了未找到用户的响应。如果任何人知道解决方案请帮助..我认为问题在于+ symbol.so的编码如何向服务器发送包含+符号的密码?这是我发布网址的代码。'

    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 = new 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();
                }
            }
        }      

2 个答案:

答案 0 :(得分:2)

由于格式错误(在发布问题时出现问题),您的代码几乎无法读取,但您可能需要对其进行URL编码才能正确读取。

实际上,如果有可能包含在a-zA-Z0-9之外的字符,则应始终对您在请求的URL中发送的所有参数(单独)进行URL编码。只需将URLEncoder.encode()方法应用于您的密码字符串,您就应该好了。

编辑:格式化得到修复,我的假设是正确的。您应该设置“password =”+ URlEncode.encode(密码),最好使用正确的编码作为第二个参数。

答案 1 :(得分:0)

更换

serverMsg="email="+email+"&password="+password;

使用

serverMsg="email="+URLEncoder.encode(email, "utf-8")+"&password="+URLEncoder.encode(password, "utf-8");