使用.htaccess与android应用程序连接到php页面

时间:2014-03-17 18:53:07

标签: php android .htaccess connection

我已经通过.htaccess文件锁定了php页面(用户名和密码),如何使用我的Android应用程序连接到这个php?我的连接功能是:

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 个答案:

答案 0 :(得分:1)

您可以在HttpPost对象中添加此代码以进行BASIC身份验证:

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