【PB续命05】WinHttp.WinHttpRequest的介绍与使用

0 WinHttp.WinHttpRequest简介

winhttp.winhttprequest是Windows操作系统中的一个API函数,用于创建和发送HTTP请求。它可以用于从Web服务器获取数据,或将数据发送到Web服务器。该函数提供了许多选项,例如设置请求头、设置代理服务器、设置超时时间等。它是一个非常强大的工具,可以帮助开发人员轻松地与Web服务器进行通信。
winhttp.winhttprequest.5.1

wihttp.winhttprequest.5.1是一个Microsoft Windows操作系统中的COM组件,用于在应用程序中进行HTTP请求和响应。它提供了一种简单的方法来与Web服务器进行通信,可以用于发送和接收HTTP请求和响应,支持各种HTTP方法,如GET、POST、PUT、DELETE等。它还提供了一些其他功能,如设置请求头、设置代理服务器、设置超时等。

1 创建PB窗口

为了演示winhttp.winhttprequest的使用创建一PB测试窗口w_restful如下:

窗口w_restful代码如下:

复制代码
forward
global type w_restful from window
end type
type mle_resp from multilineedit within w_restful
end type
type st_3 from statictext within w_restful
end type
type cb_delete from commandbutton within w_restful
end type
type cb_put from commandbutton within w_restful
end type
type cb_post from commandbutton within w_restful
end type
type cb_get from commandbutton within w_restful
end type
type sle_data from singlelineedit within w_restful
end type
type sle_url from singlelineedit within w_restful
end type
type st_2 from statictext within w_restful
end type
type st_1 from statictext within w_restful
end type
end forward

global type w_restful from window
integer width = 3186
integer height = 1980
boolean titlebar = true
string title = "PB请求RESTful的数据"
boolean controlmenu = true
boolean minbox = true
boolean maxbox = true
boolean resizable = true
long backcolor = 67108864
string icon = "AppIcon!"
boolean center = true
mle_resp mle_resp
st_3 st_3
cb_delete cb_delete
cb_put cb_put
cb_post cb_post
cb_get cb_get
sle_data sle_data
sle_url sle_url
st_2 st_2
st_1 st_1
end type
global w_restful w_restful

forward prototypes
public subroutine wf_https (string as_method)
end prototypes

public subroutine wf_https (string as_method);string ls_url,ls_data
ls_url = sle_url.text
ls_data = sle_data.text

string ls_ret 

ls_ret = gf_https_apex(ls_url,ls_data,as_method)

mle_resp.text = ls_ret
end subroutine

on w_restful.create
this.mle_resp=create mle_resp
this.st_3=create st_3
this.cb_delete=create cb_delete
this.cb_put=create cb_put
this.cb_post=create cb_post
this.cb_get=create cb_get
this.sle_data=create sle_data
this.sle_url=create sle_url
this.st_2=create st_2
this.st_1=create st_1
this.Control[]={this.mle_resp,&
this.st_3,&
this.cb_delete,&
this.cb_put,&
this.cb_post,&
this.cb_get,&
this.sle_data,&
this.sle_url,&
this.st_2,&
this.st_1}
end on

on w_restful.destroy
destroy(this.mle_resp)
destroy(this.st_3)
destroy(this.cb_delete)
destroy(this.cb_put)
destroy(this.cb_post)
destroy(this.cb_get)
destroy(this.sle_data)
destroy(this.sle_url)
destroy(this.st_2)
destroy(this.st_1)
end on

event resize;//窗口控件自动适用

long llw , llh
llw = this.workspacewidth( ) - sle_url.x 

llh = this.workspaceheight( ) - mle_resp.y

sle_url.width = llw

sle_data.width = llw

mle_resp.width = llw
mle_resp.height = llh
end event

type mle_resp from multilineedit within w_restful
integer x = 411
integer y = 248
integer width = 2158
integer height = 812
integer taborder = 40
integer textsize = -9
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
long textcolor = 33554432
string text = "responseText"
borderstyle borderstyle = stylelowered!
end type

type st_3 from statictext within w_restful
integer x = 78
integer y = 256
integer width = 302
integer height = 76
integer textsize = -9
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
long textcolor = 33554432
long backcolor = 67108864
string text = "Response:"
alignment alignment = Right!
boolean focusrectangle = false
end type

type cb_delete from commandbutton within w_restful
integer x = 14
integer y = 784
integer width = 375
integer height = 116
integer taborder = 60
integer textsize = -9
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
string text = "DELETE"
end type

event clicked;//DELETE
wf_https("DELETE")
end event

type cb_put from commandbutton within w_restful
integer x = 14
integer y = 648
integer width = 375
integer height = 116
integer taborder = 50
integer textsize = -9
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
string text = "PUT"
end type

event clicked;//PUT
wf_https("PUT")
end event

type cb_post from commandbutton within w_restful
integer x = 14
integer y = 512
integer width = 375
integer height = 116
integer taborder = 40
integer textsize = -9
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
string text = "POST"
end type

event clicked;//POST
wf_https("POST")
end event

type cb_get from commandbutton within w_restful
integer x = 14
integer y = 376
integer width = 375
integer height = 116
integer taborder = 30
integer textsize = -9
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
string text = "GET"
end type

event clicked;//GET
wf_https("GET")
end event

type sle_data from singlelineedit within w_restful
integer x = 411
integer y = 128
integer width = 2167
integer height = 100
integer taborder = 20
integer textsize = -9
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
long textcolor = 33554432
string text = "{}"
borderstyle borderstyle = stylelowered!
end type

type sle_url from singlelineedit within w_restful
integer x = 411
integer y = 4
integer width = 2167
integer height = 100
integer taborder = 10
integer textsize = -9
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
long textcolor = 33554432
string text = "https://apex.oracle.com/pls/apex/blma/b5217/"
borderstyle borderstyle = stylelowered!
end type

type st_2 from statictext within w_restful
integer x = 137
integer y = 136
integer width = 242
integer height = 76
integer textsize = -9
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
long textcolor = 33554432
long backcolor = 67108864
string text = "DATA:"
alignment alignment = Right!
boolean focusrectangle = false
end type

type st_1 from statictext within w_restful
integer x = 137
integer y = 28
integer width = 242
integer height = 76
integer textsize = -9
integer weight = 400
fontcharset fontcharset = ansi!
fontpitch fontpitch = variable!
fontfamily fontfamily = swiss!
string facename = "Tahoma"
long textcolor = 33554432
long backcolor = 67108864
string text = "URL:"
alignment alignment = Right!
boolean focusrectangle = false
end type

2 创建全局函数gf_https_apex()

用来发送请求和获得响应

复制代码
global type gf_https_apex from function_object
end type

forward prototypes
global function string gf_https_apex (string as_url, string as_data, string as_method)
end prototypes

global function string gf_https_apex (string as_url, string as_data, string as_method);// 用WinHttp.WinHttpRequest.5.1组件,请求https网页和响应

/* 调用参数示例 
String ls_url,ls_data,ls_response
ls_url = 'https://datacenter-web.eastmoney.com/api/data/v1/get?'
ls_data = "callback=jQuery11230903969942572268_1651624511350&sortColumns=UPDATE_DATE,SECURITY_CODE&sortTypes=-1,-1&pageSize=50&pageNumber=1&reportName=RPT_LICO_FN_CPD&columns=ALL&filter=(REPORTDATE='2023-09-30')"
ls_response = gf_https_apex(ls_url,ls_data,ls_method)
*/

String ls_url,ls_data,ls_response

//请求的URL和数据
ls_url = as_url 
ls_data = as_data

OleObject lole_https   //声明ole
lole_https = CREATE oleobject 

long llr
any la
llr = lole_https.ConnectToNewObject("WinHttp.WinHttpRequest.5.1") 
lole_https.setTimeouts (50000,50000,50000,10000)
la = lole_https.open (as_method,ls_url, false)    //OPEN 

if not upper(as_method) = "DELETE" then 
	lole_https.setRequestHeader ("Content-Type", "application/json")
	lole_https.setRequestHeader ('Content-Length',string(len(ls_data)))
end if

lole_https.setRequestHeader ("charset","UTF-8")
lole_https.setRequestHeader ("User-Agent","Mozilla/5.0") //这个是Apex的RESTful需要的

lole_https.Send(ls_data) 
/*
关键是这一步:如果你的https的环境没有安装浏览器的认证的根证书,一定会报错退出,
如果你申请安装了verisign等权威机构发的根证书,用ie打开上面的https,就不会报红色警告(问你是不是继续)。
但pb不会提示你是不是继续,直接报错退出。verisign的证书一年好像要1万-10万元的服务费。在这里你如果想测试一下,
你可以把上面的https地址换成https://www.alipay.com/  (支付宝的),不会报错的。注意:这里的证书及验证都是单向验证,不是双向的。
*/

IF lole_https.Status >= 300 THEN 
	ls_response = string(lole_https.Status) + " : " + string(lole_https.StatusText)
	MessageBox ("HTTPs request failed:",ls_response ) 
	return ls_response
END IF 

blob lb_body
lb_body = lole_https.responseBody

ls_response = string(lb_body,EncodingUTF8!) //返回结果

//销毁 ole
destroy lole_https

return ls_response

end function

3 运行效果

窗口上的调用https请求的函数代码:

复制代码
string ls_url,ls_data
ls_url = sle_url.text
ls_data = sle_data.text

string ls_ret 

ls_ret = gf_https_apex(ls_url,ls_data,as_method)

mle_resp.text = ls_ret

3.1 GET请求数据

【GET】按钮上的代码:

复制代码
//GET
wf_https("GET")

数据库表上的数据:

3.2 POST添加数据

【POST】按钮上的代码:

复制代码
//POST
wf_https("POST")


3.3 PUT修改数据

【PUT】按钮上的代码:

复制代码
//PUT
wf_https("PUT")

3.4 DELETE删除数据

【DELETE】按钮上的代码:

复制代码
//DELETE
wf_https("DELETE")


4 Apex的AutoREST的其它服务

  • GET一条记录
  • GET分页设置
  • 查询条件GET数据
  • 按查询条件DELETE数据
  • 批量从本地文件加载数据
  • 等等,不再举例演示了
相关推荐
淡泊if8 小时前
RESTful API设计标准:单体 vs 微服务的最佳实践
后端·微服务·restful
小二·9 小时前
Go 语言系统编程与云原生开发实战(第3篇):企业级 RESTful API 开发 —— 中间件、验证、文档与权限控制
云原生·golang·restful
码农幻想梦2 天前
实验九 Restful和ajax实现
后端·ajax·restful
AIFQuant3 天前
如何通过股票数据 API 计算 RSI、MACD 与移动平均线MA
大数据·后端·python·金融·restful
哪里不会点哪里.4 天前
Spring MVC Restful 风格的接口流程是什么样的呢?
spring·mvc·restful
提笔了无痕4 天前
Web中Token验证如何实现(go语言)
前端·go·json·restful
张心独酌6 天前
学习Rust:实现RESTful 任务管理 API(Todo API)
学习·rust·restful
fantasy5_56 天前
深入理解 Linux 动静态库:制作、原理与加载机制
linux·运维·restful
TDengine (老段)6 天前
TDengine REST API 使用手册
大数据·数据库·物联网·restful·时序数据库·tdengine·涛思数据
AIFQuant6 天前
2026 全球股市实时行情数据 API 对比指南
python·websocket·金融·数据分析·restful