• 【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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297

    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
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65

    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
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.1 GET请求数据

    【GET】按钮上的代码:

    //GET
    wf_https("GET")
    
    • 1
    • 2

    在这里插入图片描述
    数据库表上的数据:
    在这里插入图片描述

    3.2 POST添加数据

    【POST】按钮上的代码:

    //POST
    wf_https("POST")
    
    • 1
    • 2

    在这里插入图片描述
    在这里插入图片描述

    3.3 PUT修改数据

    【PUT】按钮上的代码:

    //PUT
    wf_https("PUT")
    
    • 1
    • 2

    在这里插入图片描述

    3.4 DELETE删除数据

    【DELETE】按钮上的代码:

    //DELETE
    wf_https("DELETE")
    
    • 1
    • 2


    在这里插入图片描述

    4 Apex的AutoREST的其它服务

    • GET一条记录
    • GET分页设置
    • 查询条件GET数据
    • 按查询条件DELETE数据
    • 批量从本地文件加载数据
    • 等等,不再举例演示了
  • 相关阅读:
    【Node.js】深度解析常用核心模块-path模块
    力扣刷题day44|309最佳买卖股票时机含冷冻期、714买卖股票的最佳时机含手续费
    【C语言】指针和数组笔试题解析(1)
    中国核动力研究设计院使用 DolphinDB 替换 MySQL 实时监控仪表
    基于SSM实现的儿童疫苗信息管理系统设计与实现毕业设计源码311930
    【numpy】np.random
    MySQL 5.7限制general_log日志大小
    M.2接口电路设计
    Seata AT模式源码解析一(Seata Server端启动流程)
    VUE3项目启动过程中遇到的问题记录
  • 原文地址:https://blog.csdn.net/sufuq/article/details/134445010