81 lines
2.2 KiB
Lua
81 lines
2.2 KiB
Lua
|
|
local httpc = require "http.httpc"
|
||
|
|
local dns = require "skynet.dns"
|
||
|
|
local skynet = require "skynet"
|
||
|
|
local oo = require "Class"
|
||
|
|
|
||
|
|
local log = require "Log"
|
||
|
|
|
||
|
|
local HttpClient = oo.class()
|
||
|
|
|
||
|
|
function HttpClient:Init()
|
||
|
|
|
||
|
|
end
|
||
|
|
|
||
|
|
function HttpClient:Get( url , param ,protocol )
|
||
|
|
httpc.timeout = 300 -- set timeout 1 second
|
||
|
|
protocol = protocol or "http"
|
||
|
|
local respheader = {}
|
||
|
|
local host = string.format("%s://%s", protocol,url)
|
||
|
|
log.info("Getting... ", protocol , host..param)
|
||
|
|
local status, body = httpc.get(host, param, respheader)
|
||
|
|
log.info("返回信息", host..param,status,body)
|
||
|
|
return status,body
|
||
|
|
end
|
||
|
|
|
||
|
|
function HttpClient:Post( url ,param,form,protocol )
|
||
|
|
httpc.timeout = 300 -- set timeout 1 second
|
||
|
|
log.info("POST "..url)
|
||
|
|
protocol = protocol or "http"
|
||
|
|
local respheader = {}
|
||
|
|
local host = string.format("%s://%s", protocol,url)
|
||
|
|
log.info("Posting... ".. host..param)
|
||
|
|
log.info("Post data ", skynet.server.common:TableToString(form))
|
||
|
|
local status, body = httpc.post(host, param, form , respheader)
|
||
|
|
for k,v in pairs(respheader) do
|
||
|
|
print(k,v)
|
||
|
|
end
|
||
|
|
log.info("返回信息", status,body)
|
||
|
|
return status,body
|
||
|
|
end
|
||
|
|
|
||
|
|
function HttpClient:PostJson( url ,param, jsonData ,protocol )
|
||
|
|
function Do( url ,param, jsonData ,protocol)
|
||
|
|
httpc.timeout = 300 -- set timeout 3 second
|
||
|
|
protocol = protocol or "http"
|
||
|
|
local respheader = {}
|
||
|
|
local host = string.format("%s://%s", protocol,url)
|
||
|
|
log.info("PostJson ".. host..param)
|
||
|
|
--log.info("PostJson data ",jsonData)
|
||
|
|
local status, body = httpc.postJson(host, param, jsonData , respheader)
|
||
|
|
for k,v in pairs(respheader) do
|
||
|
|
--print(k,v)
|
||
|
|
end
|
||
|
|
log.info("Web返回信息", status,body)
|
||
|
|
return status,body
|
||
|
|
end
|
||
|
|
|
||
|
|
local isSuc,status,body = pcall( Do, url ,param, jsonData ,protocol )
|
||
|
|
if not isSuc then
|
||
|
|
log.info("PostJson请求失败",url)
|
||
|
|
end
|
||
|
|
return status,body
|
||
|
|
end
|
||
|
|
|
||
|
|
|
||
|
|
--[[
|
||
|
|
function Do( host, param, jsonData , respheader )
|
||
|
|
local status, body = httpc.postJson(host, param, jsonData , respheader)
|
||
|
|
for k,v in pairs(respheader) do
|
||
|
|
print(k,v)
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
local isSuc,err = pcall(Do, host, param, jsonData , respheader )
|
||
|
|
if not isSuc then
|
||
|
|
log.info("XXXXXXXXXXXXX",err)
|
||
|
|
end
|
||
|
|
log.info("返回信息", status,body)
|
||
|
|
]]
|
||
|
|
skynet.server.httpClient = HttpClient
|
||
|
|
|
||
|
|
return HttpClient
|