Re: Euphoria CGI
- Posted by jmduro Apr 23, 2017
- 3721 views
I got it work with WinHTTP also
include std/dll.e include std/machine.e include std/math.e include std/filesys.e include std/console.e include lib/_conv_.e include lib/_debug_.e include lib/_w32errors_.e include lib/_winhttp_constants_.e include lib/_winhttp_.e atom hSession, hConnect, hRequest hSession = NULL hConnect = NULL hRequest = NULL procedure free_pointers() integer bResults if hRequest != NULL then bResults = WinHttpCloseHandle(hRequest) end if if hConnect != NULL then bResults = WinHttpCloseHandle(hConnect) end if if hSession != NULL then bResults = WinHttpCloseHandle(hSession) end if end procedure object Buffer integer bResults, dwSize, status sequence UserAgent, cookies, url, headers, content atom addr f_debug = open(InitialDir & SLASH & "debug.log", "w") Buffer = "" bResults = FALSE UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0" -- Use WinHttpOpen to obtain a session handle. hSession = WinHttpOpen( UserAgent, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0) if hSession = NULL then printf(1, "Error %d: %s\n", {error_code, error_msg}) abort(1) end if printf(1, "hSession = %d\n", {hSession}) -- Specify an HTTP server. -- WinHTTP is Unicode only hConnect = WinHttpConnect( hSession, "127.0.0.1", 9515) if hConnect = NULL then printf(1, "Error %d: %s\n", {error_code, error_msg}) abort(1) end if printf(1, "hConnect = %d\n", {hConnect}) -- Create an HTTP request handle. hRequest = WinHttpOpenRequest( hConnect, "GET", "/status", NULL, WINHTTP_NO_REFERER, {WINHTTP_DEFAULT_ACCEPT_TYPES}, NULL) if hRequest = NULL then printf(1, "Error %d: %s\n", {error_code, error_msg}) abort(1) end if printf(1, "hRequest = %d\n", {hRequest}) -- Send a request. bResults = WinHttpSendRequest( hRequest, "Accept: application/json\r\nContent-Type: application/json; charset=UTF-8\r\n", NULL, 0) if bResults = NULL then printf(1, "Error %d: %s\n", {error_code, error_msg}) abort(1) end if printf(1, "bResults = %d\n", {bResults}) -- End the request. bResults = WinHttpReceiveResponse( hRequest, NULL) if bResults = NULL then printf(1, "Error %d: %s\n", {error_code, error_msg}) abort(1) end if printf(1, "bResults = %d\n", {bResults}) -- Retrieve the header. Buffer = WinHttpQueryHeaders( hRequest ) -- Print the header contents. if sequence(Buffer) then printf(1, "Header contents: \n%s", {Buffer}) cookies = WinHttpExtractCookies(Buffer, 1) analyze_object(cookies, "cookies") cookies = WinHttpExtractCookies(Buffer) analyze_object(cookies, "cookies") end if -- Retrieve page content. Buffer = "" dwSize = WinHttpQueryDataAvailable(hRequest) while dwSize do Buffer &= WinHttpReadData(hRequest, dwSize) dwSize = WinHttpQueryDataAvailable(hRequest) end while puts(1, Buffer & "\n") -- Close any open handles. free_pointers() close(f_debug) maybe_any_key()