UrlEncode, UnlDeCode (SOLVED)

UrlEncode, UnlDeCode (SOLVED)

Postby RAMESHBABU » Tue Sep 24, 2019 4:44 am

Dear Friends,

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

Code: Select all  Expand view

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
Last edited by RAMESHBABU on Tue Sep 24, 2019 12:44 pm, edited 1 time in total.
User avatar
RAMESHBABU
 
Posts: 615
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India

Re: UrlEncode, UnlDeCode

Postby Antonio Linares » Tue Sep 24, 2019 6: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
User avatar
Antonio Linares
Site Admin
 
Posts: 41335
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: UrlEncode, UrlDeCode

Postby RAMESHBABU » Tue Sep 24, 2019 6: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: Select all  Expand view

#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

*****************************************************************************
User avatar
RAMESHBABU
 
Posts: 615
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India

Re: UrlEncode, UnlDeCode

Postby Antonio Linares » Tue Sep 24, 2019 7:32 am

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

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41335
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: UrlEncode, UnlDeCode

Postby cnavarro » Tue Sep 24, 2019 7:34 am

Dear Ramesh
Try with
Code: Select all  Expand view

   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
User avatar
cnavarro
 
Posts: 6501
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: UrlEncode, UnlDeCode

Postby RAMESHBABU » Tue Sep 24, 2019 7: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
User avatar
RAMESHBABU
 
Posts: 615
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India

Re: UrlEncode, UnlDeCode

Postby cnavarro » Tue Sep 24, 2019 8: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
User avatar
cnavarro
 
Posts: 6501
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: UrlEncode, UnlDeCode

Postby RAMESHBABU » Tue Sep 24, 2019 8:26 am

Dear Cristobal,

I sent to your personal mail.

Please check it.

My best Regards,

-Ramesh Babu P
User avatar
RAMESHBABU
 
Posts: 615
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India

Re: UrlEncode, UnlDeCode

Postby AntoninoP » 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: Select all  Expand view
//#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
 
AntoninoP
 
Posts: 375
Joined: Tue Feb 10, 2015 9:48 am
Location: Albenga, Italy

Re: UrlEncode, UnlDeCode

Postby RAMESHBABU » 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: Select all  Expand view

#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

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

User avatar
RAMESHBABU
 
Posts: 615
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India

Re: UrlEncode, UnlDeCode

Postby RAMESHBABU » 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
User avatar
RAMESHBABU
 
Posts: 615
Joined: Fri Oct 21, 2005 5:54 am
Location: Secunderabad (T.S), India


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot], karinha, Silvio.Falconi and 19 guests