Sign in to follow this  
Followers 0
txn

DllStructCreate

6 posts in this topic

#1 ·  Posted (edited)

I'm trying to use _WiniInet_HttpQueryInfo to get the headers returned by a server in response to a GET/POST request.

I've got this code but it's not working. What am I doing wrong?

    $iHeaders = Default
    $avHeaders = _WinINet_HttpQueryInfo($hHttpOpenRequest, $HTTP_QUERY_RAW_HEADERS,0)


    If Not @error Then
        ConsoleWriteLog("_WinINet_HttpQueryInfo", @error, @extended, "[" & DllStructGetPtr($avHeaders[0]) & ", " & $avHeaders[1] & "]")

        $iHeaders = DllStructGetData(DllStructCreate("char[1024]" , DllStructGetPtr($avHeaders[0])), 1)


        ConsoleWrite("--> Headers: " & $iHeaders & @CRLF)
    EndIf

I guess I'm not setting the proper struct data type, as the console outputs:

+ _WinINet_HttpQueryInfo: error: 0 extended: 0 return: [0x0166A700, 828] 
--> Headers: H

where "H" is the first char of the headers.

 

Thanks for any help.

Edited by txn

Share this post


Link to post
Share on other sites



Hi txn,

welcome on board.

Would be better you provide a running example. At first sight I 'don't like' the line

$iHeaders = DllStructGetData(DllStructCreate("char[1024]" , DllStructGetPtr($avHeaders[0])), 1)
 

.

 

See you

Sundance

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Hi Sundance,
thank you for your reply.
 
Here's the whole code.

 

#include <WinINet.au3>

Global $hInternetOpen, $hInternetConnect, $hHttpOpenRequest, $hHttpSendRequest
Global $iStartup, $iShutdown, $iInternetAttemptConnect


; start WinINet libary
$iStartup = _WinINet_Startup()
ConsoleWriteLog("_WinINet_Startup", @error, @extended, $iStartup)


; attempt a connection
$iInternetAttemptConnect = _WinINet_InternetAttemptConnect()
ConsoleWriteLog("_WinINet_InternetAttemptConnect", @error, @extended, $iInternetAttemptConnect)


; successful attempt
If $iInternetAttemptConnect Then


    ; open a connection
    $hInternetOpen = _WinINet_InternetOpen("Mozilla/5.0 Firefox/3.0.1", $INTERNET_OPEN_TYPE_PRECONFIG, 0, Default, Default)
    ConsoleWriteLog("_WinINet_InternetOpen", @error, @extended, $hInternetOpen)


    ; connect to a web server
    $hInternetConnect = _WinINet_InternetConnect($hInternetOpen, $INTERNET_SERVICE_HTTP, "www.google.com", 0, 0, Default, Default, 0)
    ConsoleWriteLog("_WinINet_InternetConnect", @error, @extended, $hInternetConnect)


    ; set up a request a file
    $hHttpOpenRequest  = _WinINet_HttpOpenRequest($hInternetConnect, "GET", "/", $INTERNET_FLAG_RELOAD, "HTTP/1.1", Default, Default, 0)
    ConsoleWriteLog("_WinINet_HttpOpenRequest", @error, @extended, $hHttpOpenRequest)


    ; send the request to the http server
    $hHttpSendRequest = _WinINet_HttpSendRequest($hHttpOpenRequest, Default, Default)
    ConsoleWriteLog("_WinINet_HttpSendRequest", @error, @extended, $hHttpSendRequest)




    $iHeaders = Default
    $avHeaders = _WinINet_HttpQueryInfo($hHttpOpenRequest, $HTTP_QUERY_RAW_HEADERS,0)


    If Not @error Then
        ConsoleWriteLog("_WinINet_HttpQueryInfo", @error, @extended, "[" & DllStructGetPtr($avHeaders[0]) & ", " & $avHeaders[1] & "]")


        $iHeaders = DllStructGetData(DllStructCreate("char[1024]" , DllStructGetPtr($avHeaders[0])), 1)


        ConsoleWrite("--> Headers: " & $iHeaders & @CRLF)
    EndIf


; end WinINet library
$iShutdown = _WinINet_Shutdown()
ConsoleWriteLog("_WinINet_Shutdown", @error, @extended, $iShutdown)


EndIf


Func ConsoleWriteLog($vMessage, $iError, $vExtended, $vReturn)
    ConsoleWrite(StringFormat("+ %s: error: %s extended: %s return: %s \r", $vMessage, $iError, $vExtended, $vReturn))
EndFunc

 

It's just a modified version of this example:  which uses _WinINet_HttpQueryInfo to get $HTTP_QUERY_CONTENT_LENGTH and it's working.

Edited by txn

Share this post


Link to post
Share on other sites

Now it works, thanks!

Share this post


Link to post
Share on other sites

But it seems not to be the whole header...

 

Sorry Sundance, I didn't notice your edit. Actually it does return the whole header, or at least it contains the "Set-Cookie" header, which is what I was looking for. However I didn't solve my problem yet.

I need my script to authenticate through https to a server which is placed inside my office intranet but it is also routed on the internet.

I managed to get the cookies on my office WinXP pc using winhttp. When I tried at home (windows 7 pc) I couldn't get the cookies in the response headers. I did a serch on google and somebody suggested it may be an issue related to using winhttp on windows 7, so I tried switching to wininet. Alas, the code you suggested works with other sites but not with my office server. So I tested a script using winhttp on windows 7 and it works with said sites.

So I guess the problem is not using winhttp on windows 7, but the server not being accessed on the intranet.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0