【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
你可以使用 Winhttp.dll 来下载图片。下面是一个使用 Winhttp.dll 下载图片的示例代码: ```cpp #include <Windows.h> #include <Winhttp.h> #include <iostream> #include <fstream> #pragma comment(lib, "Winhttp.lib") int main() { // 创建 WinHTTP 会话 HINTERNET hSession = WinHttpOpen(L"WinHTTP Example/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0); if (hSession) { // 打开连接 HINTERNET hConnect = WinHttpOpenRequest(hSession, L"GET", L"http://example.com/image.jpg", NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0); if (hConnect) { // 发送请求 if (WinHttpSendRequest(hConnect, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0)) { // 接收响应 if (WinHttpReceiveResponse(hConnect, NULL)) { // 创建文件并写入数据 std::ofstream file("image.jpg", std::ofstream::binary); if (file) { BYTE buffer[4096]; DWORD bytesRead; while (WinHttpReadData(hConnect, buffer, sizeof(buffer), &bytesRead) && bytesRead > 0) { file.write(reinterpret_cast<const char*>(buffer), bytesRead); } file.close(); std::cout << "图片下载成功!" << std::endl; } else { std::cerr << "无法创建文件!" << std::endl; } } else { std::cerr << "无法接收响应!" << std::endl; } } else { std::cerr << "无法发送请求!" << std::endl; } // 关闭连接 WinHttpCloseHandle(hConnect); } else { std::cerr << "无法打开连接!" << std::endl; } // 关闭会话 WinHttpCloseHandle(hSession); } else { std::cerr << "无法创建会话!" << std::endl; } return 0; } ``` 在上面的示例代码中,将 `http://example.com/image.jpg` 替换为你想要下载的图片的 URL。此代码将创建一个名为 "image.jpg" 的文件,并将下载的图片数据写入其中。你可以根据需要修改保存文件的路径和文件名。 请注意,此示例仅适用于 Windows 平台,并且需要链接 Winhttp.lib 库。如果你使用的是其他平台或开发环境,请相应地进行修改。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值