【发布时间】:2013-09-23 13:22:11
【问题描述】:
我服务器上的 php 文件使用 htaccess 进行密码保护。如何通过我的 android 应用向这些文件发出请求?
我无法通过谷歌搜索找到任何相关答案。
【问题讨论】:
我服务器上的 php 文件使用 htaccess 进行密码保护。如何通过我的 android 应用向这些文件发出请求?
我无法通过谷歌搜索找到任何相关答案。
【问题讨论】:
您可以在这里找到答案:
Basic HTTP Authentication on Android
基本上:
HttpUriRequest request = new HttpGet(YOUR_URL); // Or HttpPost(), depends on your needs
String credentials = YOUR_USERNAME + ":" + YOUR_PASSWORD;
String base64EncodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP);
request.addHeader("Authorization", "Basic " + base64EncodedCredentials);
HttpClient httpclient = new DefaultHttpClient();
httpclient.execute(request);
// You'll need to handle the exceptions thrown by execute()
您可以将最后一行替换为:
已编辑:
你可以试试这样的:
try {
HttpResponse response = httpclient.execute(request);
//this is the login response, logged so you can see it - just use the second part of the log for anything you want to do with the data
Log.d("Login: Response", EntityUtils.toString(response.getEntity()));
} catch (IOException e) {
//if something went badly wrong
}
所以你可以查看你的回复,可能是解析 JSON 的问题。
【讨论】:
假设您的 PHP 文件受 HTTP Basic Authentication 保护,您可以使用这种特殊的 URL 语法请求您的 php 文件:
http://validuser:validpassword@www.domain.com/validfile.php
【讨论】:
unknown host exception