FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour UrlEncode, UnlDeCode (SOLVED)
Posts: 654
Joined: Fri Oct 21, 2005 05:54 AM
UrlEncode, UnlDeCode (SOLVED)
Posted: Tue Sep 24, 2019 04:44 AM
Dear Friends,

Can anybody please help me converting these two function as under, in FWH/XHARBOUR ?

Code (fw): Select all Collapse
string urlEncode(string str){
    string new_str = "";
    char c;
    int ic;
    const char* chars = str.c_str();
    char bufHex[10];
    int len = strlen(chars);

    for(int i=0;i<len;i++){
        c = chars[i];
        ic = c;
        // uncomment this if you want to encode spaces with +
        /*if (c==' ') new_str += '+';   
        else */if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') new_str += c;
        else {
            sprintf(bufHex,"%X",c);
            if(ic < 16) 
                new_str += "%0"; 
            else
                new_str += "%";
            new_str += bufHex;
        }
    }
    return new_str;
 }

string urlDecode(string str){
    string ret;
    char ch;
    int i, ii, len = str.length();

    for (i=0; i < len; i++){
        if(str[i] != '%'){
            if(str[i] == '+')
                ret += ' ';
            else
                ret += str[i];
        }else{
            sscanf(str.substr(i + 1, 2).c_str(), "%x", &ii);
            ch = static_cast<char>(ii);
            ret += ch;
            i = i + 2;
        }
    }
    return ret;
}


Thank you in adnvace

-Ramesh Babu
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: UrlEncode, UnlDeCode
Posted: Tue Sep 24, 2019 06:43 AM
Dear Ramesh,

Those functions are already available in Harbour, so you can copy the code to xHarbour:

https://github.com/harbour/core/blob/master/contrib/hbtip/encurlc.c
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 654
Joined: Fri Oct 21, 2005 05:54 AM
Re: UrlEncode, UrlDeCode
Posted: Tue Sep 24, 2019 06:50 AM
Mr.Antonio,

Thank you very much for your instant help.

Actually I am supposed to send SMS through SMS Gateway. I am able to send SMS
using the Gateway service provider's API in English. But when I try to send
in our Local Language, it recipient is getting some junk characters. Where as
If I send the same message using the Control Panel provided by Service Provider,
or if I compose the text and paste it in the Browser's address bar along with the
API's Url, the message is sent to the recipient with its intended text.

The following is the functionality I am using.

Can you kindly can help me in this context.

-Ramesh Babu P

Code (fw): Select all Collapse
#include "FiveWin.ch"

FUNCTION Send_SMS()

LOCAL cMessage := "ప్రియమైన తల్లి తండ్రులారా,  మీ విద్యార్ధి  ఈ రోజు అనగా 21-09-2019 న పాఠశాలకు  హాజరు కాలేదని దయచేసి గుర్తించగలరు."+;
                             "-ఇట్లు  ప్రధాన ఉపాధ్యాయుడు."+;
                             "Dear Parent, Please note that your Son is absent to the School on 21-09-2019. Please ensure his regular presence. -Principal"

LOCAL cUrl        := "http://smsgateway/apis/api.php?username=xxxxxxxxxx"&password=xxxxxxxxxx&from=AAAAAAAAAA&to=9999999999&message="+;
                             cMessage

LOCAL cRetVal

FW_SetUniCode(.T.)

cRetVal := WFReadURL(cUrl)

MsgInfo(cRetVal)

RETURN nil

*****************************************************************************

FUNCTION WFReadURL(cUrl)

LOCAL cPageContent := "Error: " + cUrl + " not found or timed out."
LOCAL oConn, oHttp

IF oHttp = nil
   oHttp := TOleAuto():New('WinHttp.WinHttpRequest.5.1')
ENDIF

TRY

  oHttp:Open("PUT", cUrl, .F.)
  oHttp:Send("Post Data")

  cPageContent := oHttp:ResponseText

  IF FILE("http.log")
     ERASE http.log
  ENDIF

CATCH

  cPageContent := "Error opening SMS Gateway"

END

RETURN cPageContent

*****************************************************************************
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: UrlEncode, UnlDeCode
Posted: Tue Sep 24, 2019 07:32 AM

Mr. Rao is the right one to help you on this

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: UrlEncode, UnlDeCode
Posted: Tue Sep 24, 2019 07:34 AM
Dear Ramesh
Try with
Code (fw): Select all Collapse
   HB_SETCODEPAGE( "UTF8" )
 Fw_SetUnicode( .T. )
.../...

and tell me
Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 654
Joined: Fri Oct 21, 2005 05:54 AM
Re: UrlEncode, UnlDeCode
Posted: Tue Sep 24, 2019 07:58 AM
Dear Cristobal,

I have set the Code Page to "8TF8", In spite of that the message is received as under.

ప్రియమైన తల్లి తండ్రులారా, మీ విద్యార్ధి ఈ రోజు అనగా 21-09-2019 న పాఠశాలకు హాజరు కాలేదని దయచేసి గుర్తించగలరు.-ఇట్లు ప్రధాన ఉపాధ్యాయుడు.Dear Parent, Please note that your Son is absent to the School on 21-09-2019. Please ensure his regular presence. -Principal
Posts: 6755
Joined: Wed Feb 15, 2012 08:25 PM
Re: UrlEncode, UnlDeCode
Posted: Tue Sep 24, 2019 08:06 AM

Where can I see the information of the api that you use?

Cristobal Navarro

Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo

El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
Posts: 654
Joined: Fri Oct 21, 2005 05:54 AM
Re: UrlEncode, UnlDeCode
Posted: Tue Sep 24, 2019 08:26 AM

Dear Cristobal,

I sent to your personal mail.

Please check it.

My best Regards,

-Ramesh Babu P

Posts: 375
Joined: Tue Feb 10, 2015 09:48 AM
Re: UrlEncode, UnlDeCode
Posted: Tue Sep 24, 2019 11:58 AM
I am using harbour, so I didn't test this code with xHarbour, but here a version without C-code of urlEncode:

Code (fw): Select all Collapse
//#include <xhb.ch> //for harbour compiling

proc main()
   hb_cdpSelect("UTF8")
   ? UrlEncode("ప్రియమైన తల్లి తండ్రులారా,  మీ విద్యార్ధి  ఈ రోజు అనగా 21-09-2019 న పాఠశాలకు  హాజరు కాలేదని దయచేసి గుర్తించగలరు.")
return

function UrlEncode(cInput)
   LOCAL i, cEncoded:=""
   for i:=1 to len(cInput)
      cEncoded+=toPercent(cInput[i])
   next
return cEncoded

func toPercent(v)
   LOCAL r:="%"
   if v<127 .and. !(v $ CHR(10)+CHR(13)+" !#$%&'()*+,/:;=?@[]"+'"%<>\^`{|}')
      return v
   endif
   v:=asc(v)
   // harbour compatible version
   //r+=("0123456789ABCDEF")[hb_bitand(hb_bitShift(v,-4),15)+1]
   //r+=("0123456789ABCDEF")[hb_bitand(v,15)+1]
   r+="0123456789ABCDEF"[((v >> 4) & 15)+1]
   r+="0123456789ABCDEF"[(v & 15)+1]
return r
Posts: 654
Joined: Fri Oct 21, 2005 05:54 AM
Re: UrlEncode, UnlDeCode
Posted: Tue Sep 24, 2019 12:11 PM
Hi, Mr.AntonioP,

Thank you very much for guiding me.

I could achieve my requirement as under (might be in a crude way.. )

But I will try with your code in xHarbour and let u know the result.

Code (fw): Select all Collapse
#include "FiveWin.ch"

FUNCTION Send_SMS()

LOCAL cMessage := "Hi, ప్రియమైన తల్లి తండ్రులారా, మీ విద్యార్ధి ఈ రోజు అనగా 21-09-2019 న పాఠశాలకు హాజరు కాలేదని దయచేసి గుర్తించగలరు."+;
                                                "-ఇట్లు ప్రధాన ఉపాధ్యాయుడు."+;
                  "Dear Parent, Please note that your Son is absent to the "   +;
                  "School on 21-09-2019. Please ensure his regular presence. " +;
                  "-Principal"

LOCAL cUrl     := "http://smslogin.mobi/spanelv2/api.php?username=rameshbabup"+;
                  "&password=ramesh@1209&from=AKSHAR&to=9885302775&message="

LOCAL cRetVal

FW_SetUniCode(.T.)

cMessage := Percentage_Encoding(cMessage)
cUrl     := cUrl + cMessage
cRetVal  := WFReadURL(cUrl)

MsgInfo(cRetVal)

RETURN nil

******************************************************************************
*** FUNCTION Percentage_Encoding(cString) - To Percent Encoding after      ***
***                                         Converting the Original        ***
***                                         Message with StrToHex(cMessage)***
******************************************************************************

FUNCTION Percentage_Encoding(cString)

LOCAL cConverted := "%", n, cChar

* First convert the String to Hex
cString := StrToHex(cString)

* And insert % before every two characters
FOR n = 1 TO LEN(cString) STEP 2
    cChar := SUBSTR(cString,n,2)
    cConverted += cChar+"%"
NEXT

* Finally encode the leftover Reserved Characters
cString := EnCode_Reserved(cString)

RETURN cConverted

******************************************************************************
*** FUNCTION EnCode_Reserved(cString) - To Encode Reserved characters      ***
***                                     after Percent Encoding             ***
******************************************************************************

FUNCTION EnCode_Reserved(cString)

LOCAL nPos := 0, cEncodedChar := "", n
LOCAL aReserved := {{"!","%21"},;
                    {"#","%23"},;
                    {"$","%24"},;
                    {"%","%25"},;
                    {"&","%26"},;
                    {"'","%27"},;
                    {"(","%28"},;
                    {")","%29"},;
                    {"*","%2A"},;
                    {"+","%2B"},;
                    {",","%2C"},;
                    {"/","%2F"},;
                    {":","%3A"},;
                    {";","%3B"},;
                    {"=","%3D"},;
                    {"?","%3F"},;
                    {"@","%40"},;
                    {"[","%5B"},;
                    {"]","%5D"},;
                    {CHR(12),"%0A%0D%0D0A"},;
                    {" ","%20"},;
                    {'"',"%22"},;
                    {"-","%2D"},;
                    {".","%2E"},;
                    {"<","%3C"},;
                    {">","%3E"},;
                    {"\","%5C"},;
                    {"^","%5E"},;
                    {"_","%5F"},;
                    {"`","%60"},;
                    {"{","%7B"},;
                    {"|","%7C"},;
                    {"}","%7D"},;
                    {"~","%7E"},;
                    {"£","%C2%A3"}}

FOR n = 1 TO LEN(cString)
    nPos := ASCAN(aReserved, SUBSTR(cString,n,1))
    IF nPos >0
       cString := Stuff(cString,n,3,aReserved[nPos,2])
    ENDIF
NEXT

RETURN cString

*****************************************************************************

FUNCTION WFReadURL(cUrl)

LOCAL cPageContent := "Error: " + cUrl + " not found or timed out."
LOCAL oConn, oHttp

IF oHttp = nil
   oHttp := TOleAuto():New("Msxml2.ServerXMLHTTP")
ENDIF

TRY

  oHttp:Open("PUT", cUrl, .F.)
  oHttp:Send("Post Data")

  cPageContent := oHttp:ResponseText

  IF FILE("http.log")
     ERASE http.log
  ENDIF

CATCH

  cPageContent := "Error opening SMS Gateway"

END

RETURN cPageContent

*****************************************************************************
Posts: 654
Joined: Fri Oct 21, 2005 05:54 AM
Re: UrlEncode, UnlDeCode
Posted: Tue Sep 24, 2019 12:25 PM

Hi, Mr.AntonioP,

I tested your code. It is also working fine. :D

Thank you very much for your kind help.

  • Ramesh Babu P

Continue the discussion