纯PowerBuilder实现url编码

2020-09-15  本文已影响0人  白龙马5217

如果url中包含特殊字符或汉字,需要编码后发送到服务器后台请求数据,一般浏览器都能自动url编码。

1 pb请求数据我用internetresult对象

继承internetresult创建一个Standard Class类,命名为n_cst_internet;完整代码如下:

forward
global type n_cst_internet from internetresult
end type
end forward

global type n_cst_internet from internetresult
end type
global n_cst_internet n_cst_internet

type variables
string is_data
inet in_cst_base
end variables
forward prototypes
public function integer internetdata (blob data)
public function string of_dec2hex (unsignedlong n10)
public function string of_urlencode (string as_url)
end prototypes

public function integer internetdata (blob data);//is_data   =  string(data , EncodingUTF8!) 

is_data   =  string(data,EncodingANSI!) 
//gb2312

return 1
end function

public function string of_dec2hex (unsignedlong n10);//10转16进制
//参数:n10 为10进制数
//返回:16进制字符串
//Create 2020-09-15 by 白龙马

char    HEXDIGIT[] = "0123456789ABCDEF"
string hex
long r
do while  n10> 15
    r = mod(n10,16)
    n10 /= 16
    hex = HEXDIGIT[r + 1] + hex
loop

hex = HEXDIGIT[n10 + 1] + hex
return hex
end function

public function string of_urlencode (string as_url);//url编码
//参数:url
//返回:编码后的url
//Create 2020-09-15 by 白龙马
long ll,lll,li
string ls_hex,ls_url='',cc

byte lb_array[]

lll = len(as_url)
for ll = 1 to lll
    cc = mid(as_url,ll,1)
    if match(cc,'[\-\_\.\/\?\:\=\%\(\)\,A-Za-z0-9]')=true then
        ls_hex = cc
    else        
        lb_array[] = getbytearray (blob (cc,encodingutf8!))
        ls_hex = ''
        for li = 1 to upperbound(lb_array)          
            ls_hex = ls_hex + '%' + of_dec2hex(lb_array[li])    //10转16进制
        next        
    end if  
    ls_url = ls_url + ls_hex
next

return ls_url

end function

on n_cst_internet.create
call super::create
TriggerEvent( this, "constructor" )
end on

on n_cst_internet.destroy
TriggerEvent( this, "destructor" )
call super::destroy
end on

2 pb使用n_cst_internet的get请求

//获取as_url的html
//参数:as_url
//返回:html 字符串

integer li_rtn,li_r
string ls_data,ls_url

in_cst_base = create inet
in_cst_internet = create n_cst_internet

ls_url = in_cst_internet.of_urlencode(as_url) //url编码

li_rtn = in_cst_base.geturl(ls_url,in_cst_internet)

if li_rtn = 1 then 
   ls_data = in_cst_internet.is_data
   return ls_data
else
    return ''
end if
上一篇下一篇

猜你喜欢

热点阅读