2017-06-02 3 views
0

J'essaie de faire une simple requête GET en utilisant le winhttp library en utilisant luajit et semble être bloqué. Je crée la demande, mais lorsque j'essaie de recevoir la réponse, je n'ai rien. Le code que j'ai peut être vu ci-dessousAppeler winhttp via luajit

 
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 
} 

Un échantillon de sortie du journal, je reçois lors de l'exécution est

 
[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

google utilise la redirection. Peut-être besoin d'allumer certains où – moteus

+0

essayé un tas d'URL (reddit, stackoverflow, bing) tous avec les mêmes résultats. Alors, je viens de m'enregistrer 'GetLastError()' après 'WinHttpReceiveResponse' et j'ai obtenu' ERROR_WINHTTP_INCORRECT_HANDLE_STATE'. Donc, je suppose qu'au moins me donne quelque chose à partir de maintenant. – Danny

Répondre

0

j'oublié un des fonctions les plus importantes. Je n'appelais pas WinHttpSendRequest