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窗體

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM