pbfunc外部函数扩展应用-在Powerbuilder中进行Http的GET、POST操作

利用PBFunc扩展函数进行Http的操作时,需要对n_pbfunc_http的以下几个函数进行参数设置:

of_set_URL(...)//要进行GET或POST的url,必须
of_set_ContentType(...)//设置Content-Type,可选
of_post(...)、of_get(...)//根据需要选择post操作还是get操作
如果需要utf-8编码转换的请用n_pbfunc_encode对象中的of_str2utf8函数

下面以http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?op=getCountryCityByIp这个来获取IP所在地的webservice来讲解GET和POST操作

  • GET操作

在浏览器中输入http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?op=getCountryCityByIp,页面加载完后,在页面的HTTP GET里面中看到:

GET /WebServices/IpAddressSearchWebService.asmx/getCountryCityByIp?theIpAddress=string HTTP/1.1
Host: www.webxml.com.cn

这就是我们需要调用的信息
我们只需要将Host附加到GET对应的/WebServices/....之前,并在最前面增加http://,调用代码如下:

 1 n_pbfunc_http lnv_http
 2 lnv_http.of_clear()//清空参数
 3 
 4 n_pbfunc_encode lnv_encode
 5 lnv_http.of_set_URL("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx/getCountryCityByIp?theIpAddress=136.213.185.177")
 6 
 7 Blob lblb_data
 8 string ls_error
 9 IF lnv_http.of_Get(lblb_data,ls_error) Then
10     string gbkData
11     gbkData = lnv_encode.of_utf8ToGbk(lblb_data)//由于返回来的是utf-8编码,直接显示中文会乱码
12     MessageBox("Http Get返回",gbkData)
13 Else
14     MessageBox("提示","执行失败")
15 End IF

调用成功后返回

<?xml version="1.0" encoding="utf-8"?>

<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://WebXml.com.cn/">

  <string>136.213.185.177</string>

  <string>美国  </string>

</ArrayOfString>
  • POST操作

同样道理,在页面的HTTP POST里面中看到:

POST /WebServices/IpAddressSearchWebService.asmx/getCountryCityByIp HTTP/1.1
Host: www.webxml.com.cn
Content-Type: application/x-www-form-urlencoded
Content-Length: length

theIpAddress=string

Content-Length,这个参数忽略,将Host附加到POST对应的/WebServices/....之前,并在最前面增加http://,调用of_set_ContentType来设置Content-Type,of_add_form设置theIpAddress的参数值,调用代码如下:

 1 n_pbfunc_http lnv_http
 2 lnv_http.of_clear()//清空参数
 3 lnv_http.of_set_URL("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx/getCountryCityByIp")
 4 lnv_http.of_set_ContentType("application/x-www-form-urlencoded")
 5 
 6 n_pbfunc_encode lnv_encode
 7 blob utf8
 8 utf8= lnv_encode.of_str2utf8("136.213.185.177")
 9 lnv_http.of_add_form("theIpAddress",utf8)
10 
11 Blob lblb_data
12 string ls_error
13 IF lnv_http.of_post(lblb_data,ls_error) Then
14     
15     string gbkData
16     gbkData = lnv_encode.of_utf8ToGbk(lblb_data)//由于返回来的是utf-8编码,直接显示中文会乱码
17     MessageBox("提示",gbkData)
18 Else
19     MessageBox("提示","执行失败")
20 End IF

调用成功后返回的结果与GET一样,也可以使用该页面上面是SOAP操作,有兴趣的可以自行试验(参考下载demo中w_http中ws_*按钮代码)
Post的demo代码,参考w_http窗体

 

posted @ 2015-10-25 08:21  PBER  阅读(7320)  评论(0编辑  收藏  举报