Connect to php page using .htaccess with android application

南笙酒味 提交于 2020-01-04 15:13:15

问题


i've a locked php page (username and password) by .htaccess file, how can i do to connect to this php with my android application? My function to connect is :

String conn(JSONObject json,String page){
        String result = "";
         String stringaFinale = "";
         InputStream is = null;

         final int TIMEOUT_MILLISEC = 5000;  // = 10 seconds    
        HttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
        HttpClient client = new DefaultHttpClient(httpParams);

        HttpPost request = new HttpPost("http://olbolb.org/Hihi/"+page);

        //request result type
        request.setHeader("Accept", "application/json; charset=utf-8");

      try{  StringEntity se = new StringEntity(json.toString());
           se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
           request.setEntity(se);      

           HttpResponse response = client.execute(request); 

            temp= EntityUtils.toString(response.getEntity());
            return temp;

      }
      catch(Exception e){ 
          return null; 

      }
    }

回答1:


You can add this code for BASIC authentication in HttpPost object:

HttpPost request = new HttpPost("http://olbolb.org/Hihi/"+page);
request.setHeader("Authorization", "Basic " + 
   new Base64().encodeToString("username:password".getBytes("UTF-8"), Base64.DEFAULT) );


来源:https://stackoverflow.com/questions/22462890/connect-to-php-page-using-htaccess-with-android-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!