FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Web service HTTP GET Basic Authentication howto
Posts: 300
Joined: Wed Jul 11, 2007 11:06 AM
Web service HTTP GET Basic Authentication howto
Posted: Fri Feb 10, 2017 02: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.

Posts: 9020
Joined: Thu Oct 06, 2005 08:17 PM
Re: Web service HTTP GET Basic Authentication howto
Posted: Fri Feb 10, 2017 03:24 PM
hb_base64Encode() requires the length of the string as the second parameter (at least with xHarbour):

Code (fw): Select all Collapse
hb_base64Encode("test:",5)


EMG
Posts: 300
Joined: Wed Jul 11, 2007 11:06 AM
Re: Web service HTTP GET Basic Authentication howto
Posted: Fri Feb 10, 2017 04: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.

Continue the discussion