我有一个通过 .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/