http request to password protected files

2019-09-01 15:16发布

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.

2条回答
Rolldiameter
2楼-- · 2019-09-01 15:34

Assuming your PHP files are protected by HTTP Basic Authentication, you can request your php files using this special URL syntax:

http://validuser:validpassword@www.domain.com/validfile.php
查看更多
Viruses.
3楼-- · 2019-09-01 15:41

Here you can find your answer:

Basic HTTP Authentication on Android

Basically:

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 replace the last line with:

EDITED:

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
}

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

查看更多
登录 后发表回答