Web service HTTP GET Basic Authentication howto

Web service HTTP GET Basic Authentication howto

Postby Jack » Fri Feb 10, 2017 2:50 pm

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.svc/patients/123456789/patientinfos"
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.
Jack
 
Posts: 288
Joined: Wed Jul 11, 2007 11:06 am

Re: Web service HTTP GET Basic Authentication howto

Postby Enrico Maria Giordano » Fri Feb 10, 2017 3:24 pm

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

Code: Select all  Expand view
hb_base64Encode("test:",5)


EMG
User avatar
Enrico Maria Giordano
 
Posts: 8713
Joined: Thu Oct 06, 2005 8:17 pm
Location: Roma - Italia

Re: Web service HTTP GET Basic Authentication howto

Postby Jack » Fri Feb 10, 2017 4:44 pm

Hi,

Thank you for your answer.

I continue to search and I found a solution.

oHttp:Open( 'GET', "https://demoweb/DiamicBiothequeService.svc/patients/0242888T/patientinfos", .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.
Jack
 
Posts: 288
Joined: Wed Jul 11, 2007 11:06 am


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 97 guests