Android Question Protect files on server

Emme Developer

Well-Known Member
Licensed User
Longtime User
Hi to all!
I have a folder in a server with a list of files with same format. I want to allow only the application to access to these files. I thought to set an .htaccess, but i never work in a server so i ask for an help..
How can i setup this file? How can i send a request from b4a?

This is my .htaccess
B4X:
AuthName ""
AuthType Basic
AuthUserFile /mydomain.net/user_backup/.htpasswd
Require valid-user

.htpasswd (in this case, password i encrypted is "try". When i send a request i need to send "try" or the hash?
B4X:
try:$apr1$dR4/wKlT$iSy3ULOHuu.7jAjJQC9Bi/

B4a code
B4X:
Dim j As HttpJob
    j.Initialize("",Me)
    j.Download("http://mydomain.net/user_backup/FIRST.png")
    j.GetRequest.SetHeader("Authorization","Basic")
    j.Password = ""'don't know
    j.Username ="try"
    Wait For (j) JobDone(j As HttpJob)
    LogColor(j.Success,Colors.Blue)

Thanks!
 

Emme Developer

Well-Known Member
Licensed User
Longtime User
Now htaccess is working as expected, but i get Always Error 401.. this is the error:
B4X:
ResponseError. Reason: Unauthorized, Response: <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>401 Unauthorized</title>
</head><body>
<h1>Unauthorized</h1>
<p>This server could not verify that you
are authorized to access the document
requested.  Either you supplied the wrong
credentials (e.g., bad password), or your
browser doesn't understand how to supply
the credentials required.</p>
</body></html>

I don't understand why.. password and user are right, and by calling the url in browser all works properly
 
Upvote 0

Emme Developer

Well-Known Member
Licensed User
Longtime User
Solved using this code, but i don't understand why username and password don't work..

B4X:
j.Initialize("",Me)
    Dim su As StringUtils
    Dim auth As String = su.EncodeBase64("user:password".GetBytes("UTF8"))
    j.Download("domain/user_backup/FIRST.png")
    j.GetRequest.SetHeader("Authorization","Basic "&auth)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…