Android Question "Authorization" HTTP request header with Domoticz

krissam

Member
Licensed User
Longtime User
Hello,

I'm trying to developp a small B4A application to open and close my house gate.
To do this I just need to send to my domoticz server :
B4X:
httpjob.PutString("http://domoticz-ip:port/json.htm?type=command&param=switchlight&idx=19&switchcmd=On", "" )

But I need basic authorization.
Domoticz documentation :
When using some method other than a browser to connect to Domoticz it may be necessary to do Authorization differently. Authorization over HTTP is done by setting the "Authorization" HTTP request header when sending the request to Domoticz. The value of this header is a base64 encoded string of the username and password. When connecting to Domoticz using a browser and the URL method above the browser sets the appropriate header. When creating your own app or script this is not always done for you.
  • First the username and password are combined into one string "username:password"
  • This string is encoded using the RFC2045-MIME version of base64
  • The authorization method and a space i.e. "Basic " is then put before the encoded string.
This results in a header in the following format:
Authorization: Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==

Here is the piece of code I' testing and It doesn't work : "400 Bad Request"
B4X:
Dim connection As HttpJob
Dim userPwd As String = "user:password"
Dim su As StringUtils
Dim byt() As Byte = userPwd.GetBytes("UTF8")
Dim suUserPwd As String = su.EncodeBase64(byt)

connection.Initialize("Domoticz", Me)
connection.PostString("http://www.xxx.fr:8080", "")
connection.GetRequest.SetHeader("Authorization:","Basic "&suUserPwd)

If somebody can help me.

Best regards
 

chefe82

Member
Licensed User
Longtime User
check this

B4X:
connection.Initialize("Domoticz", Me)
connection.PostString("http://www.xxx.fr:8080", "")
connection.Username = Your Username
connection.Password = Your Password
 
Upvote 0

krissam

Member
Licensed User
Longtime User
Thanks chefe82,

Why trying to make things so complicated when they can be so simple !

Now when I'll return from my office, I'll be able to open my house gate!!!

Regards
 
Upvote 0

krissam

Member
Licensed User
Longtime User
Now my application is working but I get this error few minutes after lauching it :
ResponseError. Reason: java.io.IOException: unexpected end of stream on okhttp3.Address@a387b295, Response:
JobName = Domoticz, Success = false
Error: java.io.IOException: unexpected end of stream on okhttp3.Address@a387b295
 
Upvote 0
Top