1. Clint Maxwell
  2. PowerBuilder
  3. Thursday, 17 May 2018 20:31 PM UTC

I have to open a browser and provide it a URL to navigate to. A portion of it needs to be URL Encoded (aka Percent Encoded), because it is a re-direct URL being passed along as a parameter to a login URL, and it also has parameters of its own.

I've tried the solution below (it presumes that MSScript.ocx is registered on the machine) and it works great when running from the IDE, which in my case is PB2017R2 Build 1769. There only seems to be a 32 bit version of this ocx however, so when a 64 bit executable was created the ConnectToNewObject function started failing with a return code of -3. 

Does anyone know of an alternative way to do URL Encoding from within PB code that will work in a 64 bit exe? 

 

OleObject lnv_MSScript

lnv_MSScript = Create OleObject

// Create a script object to use for URL Encoding/Decoding

li_RC = lnv_MSScript. ConnectToNewObject ("MSScriptControl.ScriptControl")
 
If li_RC = 0 Then
lnv_MSScript.language = "javascript"
Else
Messagebox('Error', 'Unable to connect to script control. Return Code = ' + string(li_RC))
Return
End If
 
 
 
 
ls_LoginURL = 'https://some.url.com/UI/Authentication/Altlogin?'
 
// Redirect URL
ls_SearchURL = '​/SomePath/SomeOtherPath/Search?'
ls_SearchArgs = 'SomeSearchArg=LuggageCombo&AnotherSearchArg=12345'
ls_Dest = '&dest=' + lnv_MSScript.Eval ( "encodeURIComponent('" + ls_SearchURL + ls_SearchArgs + "')"  )
 
//Full URL
ls_FullURL = ls_LoginURL + ls_Dest​
 
Accepted Answer
Roland Smith Accepted Answer Pending Moderation
  1. Thursday, 17 May 2018 20:53 PM UTC
  2. PowerBuilder
  3. # Permalink

See this example:

http://www.topwizprogramming.com/freecode_twitter.html

The n_oauth object has a function of_URLEncode.

Comment
  1. Clint Maxwell
  2. Wednesday, 23 May 2018 17:43 PM UTC
Thank you Roland, this has worked great. For any interested, I created an object with the relevant pieces from Roland's example project, and then pasted the export below:



 



$PBExportHeader$nc_urlencode.sru



forward



global type nc_urlencode from nonvisualobject



end type



end forward



 



global type nc_urlencode from nonvisualobject



end type



global nc_urlencode nc_urlencode



 



forward prototypes



public function string of_nbr2hex (unsignedlong aul_number, integer ai_digit)



public function long of_hex2nbr (string as_hex)



public function string of_urlencode (string as_string)



end prototypes



 



public function string of_nbr2hex (unsignedlong aul_number, integer ai_digit);



ULong lul_temp0, lul_temp1



Char lc_ret



 



If ai_digit > 0 Then



   lul_temp0 = Abs(aul_number / (16 ^ (ai_digit - 1)))



   lul_temp1 = lul_temp0 * (16 ^ (ai_digit - 1))



   If lul_temp0 > 9 Then



      lc_ret = Char(lul_temp0 + 55)



   Else



      lc_ret = Char(lul_temp0 + 48)



   End If



   Return lc_ret + of_Nbr2Hex(aul_number - lul_temp1, ai_digit - 1)



End If



 



Return ""



end function



 



public function long of_hex2nbr (string as_hex);



Integer li_work



String ls_code, ls_hex = '123456789ABCDEF'



Long ll_rc, ll_lp



 



ls_code = Reverse(Upper(as_hex))



ll_rc = len(ls_code)



 



For ll_lp = 1 To ll_rc



li_work = li_work + Pos(ls_hex, Mid(ls_code, ll_lp, 1), 1) * (16^(ll_lp - 1))



Next



 



Return li_work



end function



 



public function string of_urlencode (string as_string);



String ls_Result, ls_Character



Integer li_Ascii, li_CurChr



 



li_CurChr = 1



Do Until li_CurChr - 1 = Len(as_string)



ls_Character = Mid(as_string, li_CurChr, 1)



li_Ascii = AscA(ls_Character)



Choose Case li_Ascii



Case 48 To 57, 65 To 90, 97 To 122



// Numbers 0-9, Uppercase A-Z, Lowercase a-z



ls_Result += ls_Character



Case Else



ls_Result = ls_Result + "%" + &



of_Nbr2Hex(AscA(ls_Character), 2)



End Choose



li_CurChr ++



Loop



 



Return ls_Result



end function



 



on nc_urlencode.create



call super::create



TriggerEvent( this, "constructor" )



end on



 



on nc_urlencode.destroy



TriggerEvent( this, "destructor" )



call super::destroy



end on

  1. Helpful
There are no comments made yet.
Chris Pollach @Appeon Accepted Answer Pending Moderation
  1. Thursday, 17 May 2018 20:43 PM UTC
  2. PowerBuilder
  3. # 1

Hi Clint;

  You could try the INET object in PB if you are using v12.6 or lower. If you are on the new PB2017 release or higher, you can now use the new HTTP Client object.

HTH

Regards .. .Chris

Comment
  1. Clint Maxwell
  2. Wednesday, 23 May 2018 17:33 PM UTC
Thanks Chris. I had looked at using the httpclient object for this, but didn't really see how to supply it the url string and then get it back with the special characters url encoded (ie hex converted). Do you have a code snip that shows this?

  1. Helpful
  1. Chris Pollach @Appeon
  2. Wednesday, 23 May 2018 20:18 PM UTC


String        ls_command = "GET"                                                                            // HTTP Command

ii_rc            =    io_http.Sendrequest ( ls_command, is_state_url )

ii_rc            =    io_http.getResponseBody ( ls_data )                                        // Get Response body





Note: The GetResponseBody ( ) method can also handle the BLOB data type as well.



         You can also specify either an ANSI or Unicode encoding too.



 

  1. Helpful
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.
We use cookies which are necessary for the proper functioning of our websites. We also use cookies to analyze our traffic, improve your experience and provide social media features. If you continue to use this site, you consent to our use of cookies.