HTTP请求密码保护的文件 [英] http request to password protected files

查看:204
本文介绍了HTTP请求密码保护的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的服务器上的PHP文件是用密码保护htaccess的。我怎样才能通过我的Andr​​oid应用程序使请求这些文件?

The php files on my server are password protect with htaccess. How can I make request to these files through my android app?

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

I couldnt find any relevant answers with google search.

推荐答案

在这里你可以找到答案:

Here you can find your answer:

在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()

您可以替换最后一行:

编辑:

您可以尝试成才这样的:

You can try someting like this:

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。

So you can view your response, maybe the problem is parsing the JSON.

这篇关于HTTP请求密码保护的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆