HttpClient Post with basic auth

joe2236

Member
Licensed User
Longtime User
hi

i bought your product 2 days ago (V1.20) and now i have this difficult problem (http lib 1.05)

im using httpclient to post data to an php page on my apache server (ssl, basic auth).

-when useing a get request all is working fine - basic auth via executecredentials is working fine with my ssl php site.

-when using a post request i get error 401 - authorization required and i see 2 logging entries in my apache access_log. the first has no username and has 401 return code - the second has the correct username and return 200 but the php script didnt work - in my app i allways get 401

for me it is important to use POST requests

in Activity_Create

B4X:
HttpClient1.InitializeAcceptAll("HttpClient1")

in a sub

B4X:
Dim data As String
Dim i As InputStream
data="param1=test"
i.InitializeFromBytesArray(data.GetBytes("UTF-8"),0,data.Length)

request.InitializePost("https://server/getlist.php",i,data.GetBytes("UTF-8").Length)
request.Timeout = 10000   
If HttpClient1.ExecuteCredentials(request,1,"username","pwd")=False Then Return "process allready running"

please :sign0085:

greetings joe
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The credentials are not sent automatically. The server should return 401 (authentication required) and then in the second request the credentials are being sent (in Basic or Digest mode based on the server first response).
This is why you see the first empty request.

Can you check if the data is being sent to the server?

I will check the authentication issue on Sunday.
 

joe2236

Member
Licensed User
Longtime User
tests

hi i have tested the post from b4a with a netcat server

B4X:
nc -vv -l -p 80


first test was with curl (works with my secure php site using --insecure param and https instead of http)

B4X:
curl -v --basic -u "Max Mustermann:pwd" -d "param1=test" "http://192.168.10.1/post.php"

second test was with b4a
additional i added
B4X:
request.SetContentEncoding("UTF-8")
request.SetContentType("application/x-www-form-urlencoded")

reply from netcat was

with curl request
B4X:
POST /post.php HTTP/1.1
Authorization: Basic TWF4IE11c3Rlcm1hbm46cHdk
User-Agent: curl/7.21.0 (amd64-pc-win32) libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.
3
Host: 192.168.10.1
Accept: */*
Content-Length: 11
Content-Type: application/x-www-form-urlencoded

param1=test

with b4a request
B4X:
POST /post.php HTTP/1.1
Content-Length: 11
Content-Type: application/x-www-form-urlencoded
Content-Encoding: UTF-8
Host: 192.168.10.1
Connection: Keep-Alive

param1=test

the differences i see is http header field order different, the "Accept" header in curl, no "Authorization" header in b4a and "Connection: Keep-Alive" in b4a

when i use curl to test my apache ssl,php server i have only one entry in my apache log, when i use b4a i hav 2 entries as mentioned before.

greetings joe
 

joe2236

Member
Licensed User
Longtime User
additionally it would be good to have a

B4X:
request.setProperty( property, value )
response.getProperty(property)

method for setting various user defined http header properties like "User-Agent" ....

see sun java post basic examples

joe
 
Last edited:
Top