0

我服务器上的 php 文件使用 htaccess 进行密码保护。如何通过我的 android 应用程序向这些文件发出请求?

我无法通过谷歌搜索找到任何相关答案。

4

2 回答 2

1

在这里你可以找到你的答案:

Android 上的基本 HTTP 身份验证

基本上:

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 上。

于 2013-09-23T13:30:03.070 回答
0

假设您的 PHP 文件受 保护HTTP Basic Authentication,您可以使用以下特殊 URL 语法请求您的 php 文件:

http://validuser:validpassword@www.domain.com/validfile.php
于 2013-09-23T13:25:29.380 回答