About Monkey 2 › Forums › General Programming Discussion › Windows HttpRequest initial attempt
This topic contains 1 reply, has 2 voices, and was last updated by 
 cocon 1 year, 9 months ago.
- 
		AuthorPosts
 - 
		
			
				
July 3, 2017 at 10:44 am #9107
As monkey2 doesn’t have httprequest for windows yet I was curious weather it could be done in pure monkey2. It seems pretty close but I am a bit baffled by difference of DWORD_PTR and LPDWORD and the odd way in which Alias of CString was not a good idea. Just to increase odds of failure I am using MSVC toolchain.
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697' monkey2 win32 http requests - currently broken#Import "<libc>"#Import "<std>"#Import "<windows.h>"#Import "<wininet.h>"#Import "<libwininet.a>"ExternAlias INTERNET_PORT:IntAlias DWORD:UIntAlias LPVOID:Void PtrGlobal INTERNET_OPEN_TYPE_DIRECT:DWORDGlobal INTERNET_OPEN_TYPE_PRECONFIG:DWORDGlobal INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY:DWORDGlobal INTERNET_OPEN_TYPE_PROXY:DWORDGlobal INTERNET_SERVICE_FTP:DWORDGlobal INTERNET_SERVICE_GOPHER:DWORDGlobal INTERNET_SERVICE_HTTP:DWORDGlobal HTTP_QUERY_ACCEPT:DWORDStruct HINTERNETEndStruct DWORD_PTREndStruct LPCTSTREndStruct LPDWORDEndFunction InternetOpen:HINTERNET(agent:CString, accessType:DWORD, proxyName:CString, proxyBypass:CString, flags:DWORD)Function InternetCloseHandle:Bool(internet:HINTERNET)Function InternetConnect:HINTERNET(internet:HINTERNET, server:CString, port:int, username:CString, password:CString, service:DWORD, flags:int, context:DWORD_PTR)Function InternetReadFile:Bool(file:HINTERNET,buffer:UByte Ptr, size:UInt, bytesRead:LPDWORD)Function InternetWriteFile()Function HttpOpenRequest:HINTERNET(http:HINTERNET, verb:CString, objectName:CString, version:CString, referer:CString, types:LPCTSTR Ptr, flags:DWORD, context:DWORD_PTR)Function HttpSendRequest:Bool(request:HINTERNET, headers:CString, headersLength:DWORD, optional:LPVOID, optionalLength:DWORD)Function HttpQueryInfo:Bool(request:HINTERNET, infoLevel:DWORD, buffer:UByte ptr, bufferLength:UInt Ptr, index:DWORD)Function HttpEndRequest()PublicFunction Main()Local context:DWORD_PTRLocal acceptTypes:LPCTSTRLocal option:LPVOIDLocal host:String="wasted.nz"' Local host:String="google.com"Local page:String="/"Local internet:=InternetOpen("monkey2-httprequest/1.1.0.5", INTERNET_OPEN_TYPE_DIRECT, "", "", Null)If internet=Null Print("InternetOpen Failed")Local http:=InternetConnect(internet, host, 80, "", "", INTERNET_SERVICE_HTTP, 0, context)If http=Null Print("InternetConnect Failed")Local request:=HttpOpenRequest(http, "GET", page, "HTTP/1.1", "", Varptr acceptTypes, 0, context)If request=Null Print("HttpOpenRequest Failed")Local result1:=HttpSendRequest(request, "", 0, option, 0)Print "result1="+(result1?"True"Else"False")Local data:=New std.memory.DataBuffer(500)Local length:UInt=data.LengthLocal bytesRead:LPDWORDLocal result2:Bool=InternetReadFile(request,data.Data,data.Length,bytesRead)Print "result2="+(result2?"True"Else"False")Local raw:=data.DataFor Local i:=0 Until 12Print raw[i]NextPrint String.FromCString(data.Data,24)InternetCloseHandle(http)InternetCloseHandle(internet)EndJuly 4, 2017 at 2:47 am #9115I wonder also if there’s a good strategy to interpret Windows API data structures. They look like that they are over complicated for no reason.
Now I think that I would simply do a wrapper module in C to mess with the Win API and then expose only the good parts.
 - 
		AuthorPosts
 
You must be logged in to reply to this topic.