php - 使用 .htaccess 和 Android 应用程序连接到 php 页面

标签 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; 

      }
    }

最佳答案

您可以在 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) );

关于php - 使用 .htaccess 和 Android 应用程序连接到 php 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22462890/

相关文章:

java - Android 模拟器上的 ListView 中未显示数据

security - 通过 HTTP 强制使用 HTTPS

django - 如何将404或500或403之类的错误重定向到Apache httpd中的自定义错误页面?

javascript - PHP 作为 JavaScript : get users IP

php - 如何在 PHP 和 Azure 中调试curl

javascript - 如何在Mark.JS中获取词干突出显示?

java - Android json listview 与数组不工作

javascript - 使用 PHP、JQuery 和 AJAX 从数据库获取 JSON 格式的数据

java - onTouch 问题 - x < y 没有反应

php - 使用 PHP/Apache 限制对静态文件(html、css、img 等)的访问