HttpQuery

发送Http请求。

返回请求的应答数据,如果返回值为JSON字符串,JavaScript语言的策略中可以用JSON.parse()函数解析,C++语言的策略中可以用json::parse()函数解析。 参数options结构中如果debug设置为true,返回值为对象(JSON);如果debug设置为false,返回值为字符串。 string、object

HttpQuery(url) HttpQuery(url, options)

Http请求url。 url true string Http请求相关设置,例如可以是以下结构:

{
    method: "POST",
    body: "a=10&b=20&c=30",
    charset: "UTF-8",
    cookie: "session_id=12345; lang=en",
    profile: "chrome_103",
    debug: false,
    headers: {"TEST-HTTP-QUERY": "123"},
    timeout: 1000
}
  • profile:用来模拟浏览器tls指纹。 支持的设置有以下选项: chrome"chrome_103""chrome_104""chrome_105""chrome_106""chrome_107""chrome_108""chrome_109""chrome_110""chrome_111""chrome_112""chrome_117"、 safari"safari_15_6_1""safari_16_0""safari_ipad_15_6""safari_ios_15_5""safari_ios_15_6""safari_ios_16_0"、 firefox"firefox_102""firefox_104""firefox_105""firefox_106""firefox_108""firefox_110""firefox_117"、 opera"opera_89""opera_90""opera_91"、 zalando"zalando_android_mobile""zalando_ios_mobile"、 nike"nike_ios_mobile""nike_android_mobile"、 cloudscraper:"cloudscraper"、 mms"mms_ios"、 mesh"mesh_ios""mesh_ios_1""mesh_ios_2""mesh_android""mesh_android_1""mesh_android_2"、 confirmed"confirmed_ios""confirmed_android"、 okhttp4"okhttp4_android_7""okhttp4_android_8""okhttp4_android_9""okhttp4_android_10""okhttp4_android_11""okhttp4_android_12""okhttp4_android_13"
  • debug:设置为true时,此次HttpQuery函数调用返回完整的应答报文。设置为false时只返回应答报文Body中的数据。
  • timeout:超时设置,设置1000表示1秒钟超时。
  • charset:支持对请求的应答数据进行转码,例如:GB18030。支持常用编码。

此结构中的所有字段均为可选,例如可以不设置profile字段。

options false object

function main(){
    // 一个GET访问不带参数的例子
    var info = JSON.parse(HttpQuery("https://www.okx.com/api/v5/public/time"))
    Log(info)
    // 一个GET访问带参数的例子
    var ticker = JSON.parse(HttpQuery("https://www.okx.com/api/v5/market/books?instId=BTC-USDT"))
    Log(ticker)
}
import json
import urllib.request
def main():
    # HttpQuery不支持Python,可以使用urllib/urllib2库代替
    info = json.loads(urllib.request.urlopen("https://www.okx.com/api/v5/public/time").read().decode('utf-8'))
    Log(info)
    ticker = json.loads(urllib.request.urlopen("https://www.okx.com/api/v5/market/books?instId=BTC-USDT").read().decode('utf-8'))
    Log(ticker)
void main() {
    auto info = json::parse(HttpQuery("https://www.okx.com/api/v5/public/time"));
    Log(info);
    auto ticker = json::parse(HttpQuery("https://www.okx.com/api/v5/market/books?instId=BTC-USDT"));
    Log(ticker);
}

访问OKX公共行情API接口的例子。

function main() {
    // 本次设置代理并发送http请求,无用户名,无密码,此次http请求会通过代理发送
    HttpQuery("socks5://127.0.0.1:8889/http://www.baidu.com/")            

    // 本次设置代理并发送http请求,输入用户名和密码,仅HttpQuery当前调用生效,之后再次调用HttpQuery("http://www.baidu.com")这样不会使用代理
    HttpQuery("socks5://username:password@127.0.0.1:8889/http://www.baidu.com/")
}
# HttpQuery不支持Python,可以使用Python的urllib2库
void main() {
    HttpQuery("socks5://127.0.0.1:8889/http://www.baidu.com/");
    HttpQuery("socks5://username:password@127.0.0.1:8889/http://www.baidu.com/");
}

HttpQuery函数使用代理设置。

HttpQuery()函数只支持JavaScriptC++语言,Python语言可以使用urllib库,直接发送Http请求。 HttpQuery()主要用于访问交易所不需要签名的接口,例如行情信息等公共接口。 回测系统中可以使用HttpQuery()发送请求(只支持GET请求)获取数据。 回测时限制使用20次访问不同的URL,并且HttpQuery()访问会缓存数据, 相同的URL第二次访问时HttpQuery()函数返回缓存数据,不再发生实际网络请求。

{@fun/Global/HttpQuery_Go HttpQuery_Go}