如何使用LuaSec在HTTPS中发送

时间:2014-06-24 06:33:27

标签: https lua luasocket luasec

在我的脚本中,我使用库LuaSocket发送XML代码。这可以使用以下代码正常工作:

local request_body = (XMLHeader..XMLBody);
local response_body = {}

local res, code, response_headers = socket.http.request
 {
  url = "http://blabla.com/v01/Authenticatie.svc";
  method = "POST";
  headers = 
 {
  ["Content-Type"] = "application/soap+xml; charset=utf-8";
  ["Content-Length"] = string.len(request_body);
  ["Accept-Encoding"] = "gzip, deflate";
  ["Connection"] = "Keep-Alive";
 };
 source = ltn12.source.string(request_body);
 sink = ltn12.sink.table(response_body);
 }

但现在我将使用带有证书的protocoll HTTPS发送XML。我知道我可以使用LuaSec,但是怎么样?有人能告诉我,我如何将代码修改为HTTPS的工作代码?

1 个答案:

答案 0 :(得分:4)

只需添加:

local https = require("ssl.https")

并替换:

local res, code, response_headers = socket.http.request

local res, code, response_headers = https.request
 {
  url = "https://blabla.com/v01/Authenticatie.svc"; 

一定要安装LuaSec:

luarocks --local install luasec OPENSSL_LIBDIR=/usr/lib64/ #CentOS
# or
luarocks --local install luasec OPENSSL_LIBDIR=/usr/lib/x86_64-linux-gnu # Ubuntu