2017-06-02 70 views
0

我正在嘗試使用luajit使用winhttp library做一個簡單的GET請求,似乎被卡住了。我正在創建請求,但是當我嘗試收到響應時,我什麼也得不到。我的代碼可以在下面通過luajit調用winhttp

 
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 
+0

谷歌使用重定向。可能需要打開它的一些地方 – moteus

+0

嘗試了一堆網址(reddit,stackoverflow,bing)都有相同的結果。我在'WinHttpReceiveResponse'之後記錄了'GetLastError()'並且得到了'ERROR_WINHTTP_INCORRECT_HANDLE_STATE'。所以我想最起碼給了我一些東西從現在開始。 – Danny

回答

0

我忽略了最重要的功能之一。我沒有打電話WinHttpSendRequest