|
|
无忧插件的HttpVisit接口可以支持
函数简介:
通用网站访问,支持http/s或ws/s服务.
函数原型:
string HttpVisit(string url, string method, string headers, string body, string proxy_addr, string proxy_name, string proxy_pass)
参数定义:
字符串 url:要访问网站的url,例如:"https://www.baidu.com/"
字符串 method:访问方式,常见的有"GET","POST","PUT","DELETE"这几种,默认使用"GET".
字符串 headers:默认空,访问头数据,如果多个请用换行符隔开.
字符串 body:默认空,访问时要提交的数据,一般为"POST"表单数据,也支持其他数据提交,如图片/视频/音频/二进制.
字符串 proxy_addr:默认空,代理地址,格式为:ip:port,例如:"8.8.8.8:88"
字符串 proxy_name:默认空,代理账号,如果有的话.
字符串 proxy_pass:默认空,代理密码,如果有的话.
返回值:
字符串:
返回响应内容,如果目标服务器返回的是UTF-8格式内容,需要调用StrUtf8ToGbk()将其转字符串,如果你的编程语言需要的是宽字符串,则需要再次调用StrA2W().
示例:
// 常规GET访问
res = vu.HttpVisit("https://www.baidu.com", "GET", "", "", "", "", "");
// POST提交表单
// 构建访问头
head = "";
head += "Accept: text/html, application/xhtml+xml, */*" + std::string("\r\n");
head += "Accept-Encoding: identity, gzip, deflate" + std::string("\r\n");
head += "Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6" + std::string("\r\n");
head += "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0" + std::string("\r\n");
head += "Cache-Control: no-cache" + std::string("\r\n");
// 提交的内容类型使用json
head += "Content-Type: application/json" + std::string("\r\n");
// 构建提交数据体
body = R"({
"name": "voouer",
"message": "Hello World!"
})";
// 使用POST执行访问
res = vu.HttpVisit("https://www.baidu.com", "POST", head.data(), body.data(), "", "", "");
|
|