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

来源:互联网 发布:淘宝店铺首页装修尺寸 编辑:程序博客网 时间:2024/06/06 07:47
原文地址:http://www.cnblogs.com/wangxianjin/p/4908201.html

利用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.1Host: 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) Then10     string gbkData11     gbkData = lnv_encode.of_utf8ToGbk(lblb_data)//由于返回来的是utf-8编码,直接显示中文会乱码12     MessageBox("Http Get返回",gbkData)13 Else14     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.1Host: www.webxml.com.cnContent-Type: application/x-www-form-urlencodedContent-Length: lengththeIpAddress=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_data12 string ls_error13 IF lnv_http.of_post(lblb_data,ls_error) Then14     15     string gbkData16     gbkData = lnv_encode.of_utf8ToGbk(lblb_data)//由于返回来的是utf-8编码,直接显示中文会乱码17     MessageBox("提示",gbkData)18 Else19     MessageBox("提示","执行失败")20 End IF
复制代码

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


0 0
原创粉丝点击