1. Shenn Sellers
  2. PowerBuilder
  3. Tuesday, 7 February 2023 20:43 PM UTC

I'm trying to use the HttpClient object to get an image from an IP Camera.  Using the object for one type of camera works, but a different camera has different parameters that bring back a Http 401 error.  

I pass in the IP Address of the camera.  This works...

lnv_HttpClient.SendRequest("GET", "http://" + is_ip_camera + "/cgi-bin/encoder?USER=username&PWD=password&SNAPSHOT")

This doesn't...

lnv_HttpClient.SendRequest("GET", "http://username:password@" + is_ip_camera + "/ISAPI/Streaming/channels/1/picture")

I'm assuming it has to do with passing the username and password at the beginning of the URL instead of at the end.

If I put this in a browser it comes back fine.  "http://username:password@IPAdress/ISAPI/Streaming/channels/1/picture"

How can I get the above to work with the HttpClient Object?  The code below is how I create and populate the HttpClient Object.  Thanks.

HttpClient lnv_HttpClient

//Allocate space for the HTTP Client
lnv_HttpClient = Create HttpClient

//Not to read data automatically after sending request (default is true)
lnv_HttpClient.AutoReadData = false

 

Using PB 2022 Build 1878 on Windows 10

mike S Accepted Answer Pending Moderation
  1. Tuesday, 7 February 2023 22:41 PM UTC
  2. PowerBuilder
  3. # 1

"I'm assuming it has to do with passing the username and password at the beginning of the URL instead of at the end."

yes, the url is where to go - sort of like a function name.  Anything after the ? in the url is the arguments for the 'function'.

 

 

Comment
  1. Shenn Sellers
  2. Tuesday, 7 February 2023 23:11 PM UTC
I tried that. It brings back errors.
  1. Helpful
  1. Roland Smith
  2. Wednesday, 8 February 2023 00:50 AM UTC
It would be helpful if we knew what kind of camera it was and had a link to the manufacturers documentation.
  1. Helpful
There are no comments made yet.
Daryl Foster Accepted Answer Pending Moderation
  1. Tuesday, 7 February 2023 23:52 PM UTC
  2. PowerBuilder
  3. # 2

Hi Shenn, the URL format http://username:password@IPAdress/ISAPI/Streaming/channels/1/picture is a special format that uses username and password for Basic Authentication. It is possible that HttpClient doesn't support that format of addressing.  You could try to add the Basic Auth headers yourself and see if that works.

The other thing to be mindful of is that if your password (or username) contains the ~ character, it will need to be escaped.  e.g. if your password was something~secret, you would need to assign it as "something~~secret", not "something~secret".

 

HttpClient lnv_HttpClient

//Allocate space for the HTTP Client
lnv_HttpClient = Create HttpClient

//Not to read data automatically after sending request (default is true)
lnv_HttpClient.AutoReadData = false

// Using Basic Auth to access the url
// Encode the username:password as a base64 encoded string
string ls_basic
CoderObject lo_Coder

lo_Coder = Create CoderObject
ls_basic = lo_Coder.base64encode( Blob("username:password" , EncodingUTF8!))

// Add the Authorization header
lnv_HttpClient.SetRequestHeader( "Authorization", "Basic " + ls_basic)

// Call the url without the username:password@
lnv_HttpClient.SendRequest("GET", "http://" + is_ip_camera + "/ISAPI/Streaming/channels/1/picture")

 

 

Comment
  1. Daryl Foster
  2. Wednesday, 8 February 2023 01:34 AM UTC
That's strange. You've definitely got the correct username and password, and the password doesn't have a ~ character?



Have you tried with https:// rather than http://?
  1. Helpful
  1. Shenn Sellers
  2. Wednesday, 8 February 2023 03:09 AM UTC
Yes. The username and password are nothing fancy and only use letters and numbers. Haven't tried https, I can give it a go at work tomorrow.



Does anyone at Appeon know if the HttpClient supports that format of addressing?
  1. Helpful
  1. Daryl Foster
  2. Wednesday, 8 February 2023 03:43 AM UTC
No worries, good luck. I doubt the https will make a difference, but it's worth a shot. The 401 error is an authentication error, so I think it looks like there is definitely a problem with recognizing the password or the username. If you are getting the same error with using the Authorization header and using the username:password format of the url I think the HttpClient does support that format of addressing, otherwise it probably would have given a different error. Can you post your full code above for us to look at?
  1. Helpful
There are no comments made yet.
mike S Accepted Answer Pending Moderation
  1. Wednesday, 8 February 2023 15:42 PM UTC
  2. PowerBuilder
  3. # 3

I would use postman or snapdevelop's web api tester (under the test menu item) to get the api to work. 

 

Comment
There are no comments made yet.
Kevin Ridley Accepted Answer Pending Moderation
  1. Wednesday, 8 February 2023 16:12 PM UTC
  2. PowerBuilder
  3. # 4

I agree with Mike, try it in PostMan.  Once you have it working in PostMan, use the same code in the HTTPClient.  You can view what PostMan sends and duplicate it.

Comment
There are no comments made yet.
  • Page :
  • 1


There are no replies made for this question yet.
However, you are not allowed to reply to this question.