zoukankan      html  css  js  c++  java
  • 解决openresty http客户端不支持https的问题

     OpenResty默认没有提供Http客户端,需要使用第三方提供;当然我们可以通过ngx.location.capture 去方式实现,但它只能发送一个子请求。

    第三方基本是以lua-resty-http为代表,这个类库如果去访问http和正规的https是没有问题,也挺好用的,但如果访问使用山寨证书的请求会出一些错误,比如:handshake failed,socket error等等之类的错误。对于种我的解决办法是使用curl,可以很好解决这个问题,现在来看算是比较完美的。
    具体代码如下:

    local curl = require("luacurl")
    
    local function postJson(url,postData,c)
        local result = { }
        if c == nil then
            c = curl.new()
        end
        c:setopt(curl.OPT_URL, url)
        c:setopt(curl.OPT_SSL_VERIFYHOST,0)
        c:setopt(curl.OPT_SSL_VERIFYPEER,false)
        c:setopt(curl.OPT_POST,true)
        c:setopt(curl.OPT_HTTPHEADER, "Content-Type: application/json")
        c:setopt(curl.OPT_POSTFIELDS, postData)
        c:setopt(curl.OPT_WRITEDATA, result)
        c:setopt(curl.OPT_WRITEFUNCTION, function(tab, buffer)
            table.insert(tab, buffer)
            return #buffer
        end)
        local ok = c:perform()
        return ok, table.concat(result)
    end
    
     local ok,html = postJson(serverUrl,data);
            if ok then
                ngx.say(html)
            end
  • 相关阅读:
    poj 1579(动态规划初探之记忆化搜索)
    hdu 1133(卡特兰数变形)
    CodeForces 625A Guest From the Past
    CodeForces 625D Finals in arithmetic
    CDOJ 1268 Open the lightings
    HDU 4008 Parent and son
    HDU 4044 GeoDefense
    HDU 4169 UVALive 5741 Wealthy Family
    HDU 3452 Bonsai
    HDU 3586 Information Disturbing
  • 原文地址:https://www.cnblogs.com/wfcfan/p/5257684.html
Copyright © 2011-2022 走看看