首页 文章

通过luajit调用winhttp

提问于
浏览 978
0

我正在尝试使用winhttp library使用luajit做一个简单的GET请求,似乎被卡住了 . 我正在创建请求,但是当我尝试接收响应时,我什么都没得到 . 我的代码可以在下面看到

local ffi = require("ffi")
local log = require("log")
local winhttp = ffi.load("winhttp")

ffi.cdef[[
 typedef void * HINTERNET;
 typedef unsigned int INTERNET_PORT;

 typedef const wchar_t * LPCWSTR;
 typedef unsigned long DWORD;
 typedef unsigned long DWORD_PTR;
 typedef DWORD * LPDWORD;
 typedef void * LPVOID;
 typedef int BOOL;

 static const int INTERNET_DEFAULT_PORT          = 0;
 static const int INTERNET_DEFAULT_HTTP_PORT     = 80;
 static const int INTERNET_DEFAULT_HTTPS_PORT    = 443;

 static const int WINHTTP_FLAG_SECURE            = 0x00800000;

 HINTERNET WinHttpOpen(LPCWSTR pwszUserAgent, DWORD dwAccessType, LPCWSTR pwszProxyName, LPCWSTR pwszProxyBypass, DWORD dwFlags);
 HINTERNET WinHttpConnect(HINTERNET hSession, LPCWSTR pswzServerName, INTERNET_PORT nServerPort, DWORD dwReserved);
 HINTERNET WinHttpOpenRequest(HINTERNET hConnect, LPCWSTR pwszVerb, LPCWSTR pwszObjectName, LPCWSTR pwszVersion, LPCWSTR pwszReferrer, LPCWSTR *ppwszAcceptTypes, DWORD dwFlags);
 BOOL WinHttpSendRequest(HINTERNET hRequest, LPCWSTR pwszHeaders, DWORD dwHeadersLength, LPVOID lpOptional, DWORD dwOptionalLength, DWORD dwTotalLength, DWORD_PTR dwContext);
 BOOL WinHttpReceiveResponse(HINTERNET hRequest, LPVOID lpReserved);
 BOOL WinHttpQueryDataAvailable(HINTERNET hRequest, LPDWORD lpdwNumberOfBytesAvailable);
 DWORD GetLastError(void);
]]

return {
 get = function(url)
  local session = winhttp.WinHttpOpen(L("Example/1.0"), 1, nil, nil, 0)
  log.debug(session)
  if not session then
    log.error("Could not create session ", ffi.C.GetLastError())
    return
  end
  --www.google.com
  local connect = winhttp.WinHttpConnect(session, L("www.google.com"), winhttp.INTERNET_DEFAULT_PORT, 0)
  log.debug(connect)
  if not connect then
    log.error("Could not connect ", ffi.C.GetLastError())
    return
  end
  local request = winhttp.WinHttpOpenRequest(connect, L("GET"), nil, nil, nil, nil, winhttp.WINHTTP_FLAG_SECURE)
  log.debug(request)
  if not request then
    log.error("Could not create request ", ffi.C.GetLastError())
    return
  end
  local response = winhttp.WinHttpReceiveResponse(request, nil)
  log.debug(response)
  if not tonumber(response) then
    log.error("Could not receive response ", ffi.C.GetLastError())
    return
  end
  local size = ffi.new("LPDWORD")
  winhttp.WinHttpQueryDataAvailable(request, size)
  log.debug(size)
  if not tonumber(size) or not tonumber(size[0]) then
    log.error("No data available ", ffi.C.GetLastError())
    return
  end
 end
}

运行时得到的示例日志输出是

[DEBUG Fri Jun  2 09:10:42 2017] lua\libs\http.lua:34: cdata: 0x005d16c8
[DEBUG Fri Jun  2 09:10:42 2017] lua\libs\http.lua:41: cdata: 0x0077ec60
[DEBUG Fri Jun  2 09:10:42 2017] lua\libs\http.lua:47: cdata: 0x005a2328
[DEBUG Fri Jun  2 09:10:42 2017] lua\libs\http.lua:53: -0
[DEBUG Fri Jun  2 09:10:42 2017] lua\libs\http.lua:60: cdata: NULL
[ERROR Fri Jun  2 09:10:42 2017] lua\libs\http.lua:62: No data available  183

1 回答

  • 0

    我忽视了一个最重要的功能 . 我没打电话 WinHttpSendRequest

相关问题