Web service HTTP GET Basic Authentication howto

Post Reply
Jack
Posts: 249
Joined: Wed Jul 11, 2007 11:06 am

Web service HTTP GET Basic Authentication howto

Post by Jack »

Hi all,

Could you check that code.
I'm trying to access REST web service using HTTP basic authntication, but this is not working as well.
Could you help me ?

#include "fivewin.ch"
function main()

local ohttp := CreateObject( "MSXML2.XMLHTTP" )

set date french
set century on


oHttp := CreateObject( 'Microsoft.XMLHTTP' )

cHttpSend := [{ "Username": ]+hb_base64Encode("test:")+[, "Password": ]+hb_base64Encode("12345")+[}]

cUri:="https://demosite/DiamicBiothequeService ... tientinfos"
ohttp:Open( "GET" ,cUri, .F. )
ohttp:SetRequestHeader( "Accept" , "application/json" )
ohttp:SetRequestHeader( "Content-Type" , "application/json" )
*ohttp:SetRequestHeader( "Username" , hb_base64Encode("test:"))
*ohttp:SetRequestHeader( "Password" , hb_base64Encode("12345") )
ohttp:SetRequestHeader( "Authorization" , cHttpSend )

try
ohttp:Send( cHttpSend )
catch
MsgInfo( "Connection error:" + oHttp:lastErrorMessage())
end
msgalert( alltrim(ohttp:responseText) )

return .T.


Thank you in advance.
kind regards.

Jack.
User avatar
Enrico Maria Giordano
Posts: 7355
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia
Contact:

Re: Web service HTTP GET Basic Authentication howto

Post by Enrico Maria Giordano »

hb_base64Encode() requires the length of the string as the second parameter (at least with xHarbour):

Code: Select all

hb_base64Encode("test:",5)
EMG
Jack
Posts: 249
Joined: Wed Jul 11, 2007 11:06 am

Re: Web service HTTP GET Basic Authentication howto

Post by Jack »

Hi,

Thank you for your answer.

I continue to search and I found a solution.

oHttp:Open( 'GET', "https://demoweb/DiamicBiothequeService. ... tientinfos", .f. )
oHttp:SetRequestHeader( "Content-Type","application/json") /* if its json*/
ohttp:SetRequestHeader( "Authorization", "Basic "+hb_base64Encode("user:pwrd"))
oHttp:Send()

This code is working but I have just one blocking step.
I receive a security alert to accept the certificate.
Is this a way to avoid this certificate alert ?

Like it is done in .Net with this code :

private static void SetCertificatePolicy()
{
ServicePointManager.ServerCertificateValidationCallback += ValidateRemoteCertificate;
}

/// <summary>
/// Certificate validation callback.
/// </summary>
private static bool ValidateRemoteCertificate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
{
//Debug.WriteLine("Trusting X509Certificate '" + cert.Subject + "'");
return true;
}

Thank you.

Kind regards.
Jack.
Post Reply