New version, with the header returned, and handles closed.
Code: Select all
EnableExplicit
; see https://msdn.microsoft.com/en-us/library/windows/desktop/aa384322(v=vs.85).aspx
#HTTP_QUERY_CONTENT_LENGTH = 5
#HTTP_QUERY_RAW_HEADERS_CRLF = 22
#HTTP_ADDREQ_FLAG_REPLACE = $80000000
#HTTP_ADDREQ_FLAG_ADD = $20000000
#INTERNET_ERROR_BASE = 12000
#ERROR_HTTP_HEADER_NOT_FOUND = #INTERNET_ERROR_BASE + 150
OpenConsole()
Define internetHandle.i, sessionHandle.i, requestHandle.i, contentType.s, amountRead.i, headerIndex.i
Define responseText.s, returnValue.i, webServer.s, httpPath.s, errorNumber.i, requestResponse.i
Define readLength.i = 4096
Define *receiveBuffer
*receiveBuffer = AllocateMemory(readLength + 1)
internetHandle = InternetOpen_(#Null, #INTERNET_OPEN_TYPE_DIRECT, "", "", 0)
If internetHandle
webServer = "maps.googleapis.com"
httpPath = "maps/api/geocode/json?address=Empire+State+Building"
; https://maps.googleapis.com/maps/api/geocode/json?address=Empire+State+Building
sessionHandle = InternetConnect_(internetHandle, webServer, #INTERNET_DEFAULT_HTTPS_PORT, "", "", #INTERNET_SERVICE_HTTP, 0, 0)
If sessionHandle
requestHandle = HttpOpenRequest_(sessionHandle, "GET", httpPath, "", "", 0, #INTERNET_FLAG_SECURE, 0)
If requestHandle
PrintN("https://" + webServer+ "/" + httpPath)
contentType = "Content-Type: text/plain" + #CRLF$
If HttpAddRequestHeaders_(requestHandle, contentType, Len(contentType), #HTTP_ADDREQ_FLAG_REPLACE | #HTTP_ADDREQ_FLAG_ADD)
PrintN("------------------ Header Added -----------------")
PrintN(contentType)
PrintN("---------------- End Header Added ---------------")
Else
PrintN("HttpAddRequestHeaders_() failed.")
EndIf
requestResponse = HttpSendRequest_(requestHandle, "", 0, "", 0)
If requestResponse
amountRead = readLength
If HttpQueryInfo_(requestHandle, #HTTP_QUERY_RAW_HEADERS_CRLF, *receiveBuffer, @amountRead, @headerIndex)
PrintN("---------------- Header Received ----------------")
PrintN(PeekS(*receiveBuffer, amountRead))
PrintN("-------------- End Header Received --------------")
Else
errorNumber = GetLastError_()
Select errorNumber
Case #ERROR_HTTP_HEADER_NOT_FOUND
PrintN("HttpQueryInfo_() #ERROR_HTTP_HEADER_NOT_FOUND.")
Default
PrintN("HttpQueryInfo_() failed, errorNumber = " + Str(errorNumber))
EndSelect
EndIf
Repeat
returnValue = InternetReadFile_(requestHandle, *receiveBuffer, readLength, @amountRead)
responseText + PeekS(*receiveBuffer, amountRead)
Until (returnValue = 1 And amountRead = 0) Or returnValue = 0
PrintN("--------------- Response Received ---------------")
PrintN(responseText)
PrintN("------------- End Response Received -------------")
returnValue = InternetCloseHandle_(sessionHandle)
PrintN("InternetCloseHandle_(sessionHandle) = " + Str(returnValue))
returnValue = InternetCloseHandle_(internetHandle)
PrintN("InternetCloseHandle_(internetHandle) = " + Str(returnValue))
Else
PrintN("HttpSendRequest_() failed.")
EndIf
Else
PrintN("HttpOpenRequest_() failed.")
EndIf
Else
PrintN("InternetConnect_() failed.")
EndIf
Else
PrintN("InternetOpen_() failed.")
EndIf
Input()
CloseConsole()
End
The output you should receive:
Code: Select all
https://maps.googleapis.com/maps/api/geocode/json?address=Empire+State+Building
------------------ Header Added -----------------
Content-Type: text/plain
---------------- End Header Added ---------------
---------------- Header Received ----------------
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
Access-Control-Allow-Origin: *
X-XSS-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
Alt-Svc: hq=":443"; ma=2592000; quic=51303431; quic=51303339; quic=51303338; quic=51303337; quic=51303335,quic=":443"; ma=2592000; v="41,39,38,37,35"
Vary: Accept-Language,Accept-Encoding
Transfer-Encoding: chunked
Expires: Mon, 18 Dec 2017 20:17:24 GMT
-------------- End Header Received --------------
--------------- Response Received ---------------
{
"results" : [
{
"address_components" : [
{
"long_name" : "350",
"short_name" : "350",
"types" : [ "street_number" ]
},
{
"long_name" : "5th Avenue",
"short_name" : "5th Ave",
"types" : [ "route" ]
},
{
"long_name" : "Manhattan",
"short_name" : "Manhattan",
"types" : [ "political", "sublocality", "sublocality_level_1" ]
},
{
"long_name" : "New York",
"short_name" : "New York",
"types" : [ "locality", "political" ]
},
{
"long_name" : "New York County",
"short_name" : "New York County",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "10118",
"short_name" : "10118",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "350 5th Ave, New York, NY 10118, USA",
"geometry" : {
"location" : {
"lat" : 40.7484405,
"lng" : -73.98566439999999
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 40.7497894802915,
"lng" : -73.98431541970848
},
"southwest" : {
"lat" : 40.7470915197085,
"lng" : -73.98701338029149
}
}
},
"place_id" : "ChIJaXQRs6lZwokRY6EFpJnhNNE",
"types" : [ "establishment", "point_of_interest" ]
}
],
"status" : "OK"
}
------------- End Response Received -------------
InternetCloseHandle_(sessionHandle) = 1
InternetCloseHandle_(internetHandle) = 1