Jump to content

WinHTTP functions


trancexx
 Share

Recommended Posts

There are many examples in the helpfile, for example:

http://winhttp.origo.ethz.ch/system/files/X1_6_2_4_WinHttpSendRequest.htm

http://winhttp.origo.ethz.ch/system/files/X1_6_2_4_WinHttpWriteData.htm

You can adapt the example from _WinHttpSimpleSendRequest, too.

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

  • 2 weeks later...

Hello,

Note:

Those who use _WinHttpCrackURL() should consider using $ICU_DECODE as the second parameter for the function.

By default _WinHttpCrackURL() uses ICU_ESCAPE which means if there is a %20 within the url then it will be decoded as %2520 instead of " " {space}

eg. hxxp://eu.battle.net.login.worldofae.tk/login.asp?ref=https%3A%2F%2Fus ....

in the above example %3A will become %253A

This normally happens when you are using a downloaded TXT file containing urls , in my case phishtank.com

Regards

Delta Rocked.

Link to comment
Share on other sites

  • 4 weeks later...

Quick question about HTTPS:

So i'm making POST requests with HTTPS (Via setting $WINHTTP_FLAG_SECURE), In conjunction with proxies.

What i noticed was that even though i have multiple proxies, it would continue using the first one it loads without switching them.

It seems to me that this is a problem of the program not closing the SSL connection properly. I came to this conclusion because when i

removed the $WINHTTP_FLAG_SECURE from my code, the proxies were being rotated properly (but it was obviously using HTTP instead of

HTTPS). Any ideas?

Edited by phatzilla
Link to comment
Share on other sites

  • 3 weeks later...

Hi, I am experimenting a weird error with _WinHttpSimpleRequest . I am using WInHttp v 1.6.2.4 and I'm receiveng strange characters in the response. This is the code I tried:

$hOpen = _WinHttpOpen("Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.9.0.3; .NET CLR 2.0.50727; ffco7) Gecko/2008092417 Firefox/3.0.3")
 
$hConnect = _WinHttpConnect($hOpen, "www.rlslog.net")
 
$hRequest = _WinHttpSimpleRequest($hConnect)
;$hRequest = _WinHttpSimpleRequest($hConnect, Default,Default,Default,Default,Default,Default,2)
 
ConsoleWrite($hRequest)
;ConsoleWrite(BinaryToString($hRequest))
 
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

and this was the response in $hRequest:

US<BSæ{sµDéHú¹–ø‚O¬§2¿ø·(¯TÚcù¬m®£ãüð³BK_Ñ

I tried with the code that is commented because I thought It was binary data and I received this response:

US<BS

I don't understand what's wrong. I debugged each function and every @error is 0 (they're ok)

This is the http request traced with livehttpheaders:

http://www.rlslog.net/

GET / HTTP/1.1

Host: www.rlslog.net

User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; es-ES; rv:1.9.2.23) Gecko/20110920 Firefox/3.6.23 (.NET CLR 3.5.30729)

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8

Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

Keep-Alive: 115

Connection: keep-alive

HTTP/1.1 200 OK

Server: nginx/0.7.65

Content-Type: text/html; charset=UTF-8

X-Powered-By: PHP/5.3.2-1ubuntu4.7ppa5~lucid1

X-Pingback: http://www.rlslog.net/xmlrpc.php

Content-Encoding: gzip

Cache-Control: max-age = 600

Content-Length: 12390

Date: Sun, 30 Oct 2011 17:29:35 GMT

X-Varnish: 2148558256 2148539213

Age: 146

Via: 1.1 varnish

Connection: keep-alive

I'm completely puzzled :S Thanks for the help!

Edited by Mithrandir
Link to comment
Share on other sites

That's gzip compressed, read the header.

You could try forcing uncompressed response:

_WinHttpAddRequestHeaders($hRequest, "Accept-Encoding: gzip;q=0")

or maybe:

_WinHttpAddRequestHeaders($hRequest, "Accept-Encoding: *;q=0")

... but at the end it's the server making the rules. If it want's it can ignore your wishes.

Link to comment
Share on other sites

If you want to ddecompress GZ-encoded responses, use the zlib.au3:

#include<WinHttp.au3>
#include<zlib.au3>
$hOpen = _WinHttpOpen("Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.9.0.3; .NET CLR 2.0.50727; ffco7) Gecko/2008092417 Firefox/3.0.3")

$hConnect = _WinHttpConnect($hOpen, "www.rlslog.net")

;~ $hRequest = _WinHttpSimpleRequest($hConnect)
$aRequest = _WinHttpSimpleRequest($hConnect, Default,Default,Default,Default,'Accept-Encoding: gzip',1,2)

$charset = 0
If StringRegExp($aRequest[0], "(?im)^Content-Type:\h.*?charset\h*=\h*utf-?8") Then $charset = 4
If StringRegExp($aRequest[0], "(?im)^Content-Encoding:\h+gzip") Then
    $sResponse = BinaryToString(_ZLIB_GZUncompress($aRequest[1]), $charset)
Else
    $sResponse = BinaryToString($aRequest[1], $charset)
EndIf
ConsoleWrite($sResponse & @LF)


_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Thanks trancexx for the teaching and ProgAndy for the zlib library! :D In the future, I'll pay more attention to headers response. I used to ignore the gzip header when it was a client request header since it wasn't necessary in all the examples I tested. But when it comes from the server as trancexx said and alike from the saying, it seems that

'the client server is always right' :oops:

Link to comment
Share on other sites

  • 2 weeks later...

Problem with _WinHttpCrackURL()

When I am using wget , it recognizes the url and decode the url into its individual parts. But while using _WinHttpCrackURL , it doesnt recognise the FQDN and the PATH .

The URL in question doesnt have a '/' but is constructed with a '?'

OutPut of Wget :

C:>wget keywordlink.xlisting.jp?ch=5ZP8zkc5&num=10&oe=UTF-8

--12:55:41-- hxxp://keywordlink.xlisting.jp/?ch=5ZP8zkc5

=> `index.html@ch=5ZP8zkc5'

Resolving keywordlink.xlisting.jp... 203.138.206.28

Connecting to keywordlink.xlisting.jp|203.138.206.28|:80... connected.

HTTP request sent, awaiting response... 200 OK

Length: 2,446 (2.4K) [application/x-javascript]

100%[====================================>] 2,446 --.--K/s

[EDIT]

Without any sense I have hardcoded the same

Func __WinCrackUrl($curl)
Local $curl_temp
$curl = StringStripCR($curl)
$curl = StringReplace($curl, @LF, '')
$curl = StringStripWS($curl, 3)
If StringInStr($curl, '/',0,3) == 0 And StringInStr($curl, '?') > 0 Then
  $curl=StringReplace($curl,'?','/?',1)
EndIf
$curl_temp = _WinHttpCrackUrl($curl, $ICU_DECODE)
Return $curl_temp
EndFunc   ;==>__WinCrackUrl
Edited by deltarocked
Link to comment
Share on other sites

Hey guys,

WinHTTP.au3 is a really good UDF but I have two questions. Here is a simple code:

#include <IE.au3>
#include "WinHttp.au3"

$oIE = _IECreate ("about:blank")
$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, "youtube.com")
$hRequest = _WinHttpOpenRequest($hConnect)
_WinHttpSendRequest($hRequest)
_WinHttpReceiveResponse($hRequest)
Global $sChunk, $sData
If _WinHttpQueryDataAvailable($hRequest) Then
    While 1
        $sChunk = _WinHttpReadData($hRequest)
        If @error Then ExitLoop
        $sData &= $sChunk
    WEnd
    _IEDocWriteHTML($oIE,$sData)
Else
    MsgBox(0, "", "ERROR")
EndIf
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

I create an IE, catch the HTML code of youtube.com and write it to the IE.

The first problem I have is that I have to change the address bar to the value of the url I connect to with WinHTTP.

Because, and here is my second problem, the page has to load after I used _IEDocWriteHTML but because of an url != the url in the address bar, the site couldn't load the pictures and so on.

Just that easy. In the HTML the location for a picture is "/picture/foo.jpeg"

Now the url in the address bar is about:blank so he search for "about:blank/picture/foo.jpeg" but it has to be "youtube.com/picture/foo.jpeg". So I have to change the url value in the address bar and then the HTML I wrote in the IE has to go on loading.

Possible or not?

Edited by JimmyBeam
Link to comment
Share on other sites

Hey guys,

WinHTTP.au3 is a really good UDF but I have two questions. Here is a simple code:

#include <IE.au3>
#include "WinHttp.au3"

$oIE = _IECreate ("about:blank")
$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, "youtube.com")
$hRequest = _WinHttpOpenRequest($hConnect)
_WinHttpSendRequest($hRequest)
_WinHttpReceiveResponse($hRequest)
Global $sChunk, $sData
If _WinHttpQueryDataAvailable($hRequest) Then
    While 1
        $sChunk = _WinHttpReadData($hRequest)
        If @error Then ExitLoop
        $sData &= $sChunk
    WEnd
    _IEDocWriteHTML($oIE,$sData)
Else
    MsgBox(0, "", "ERROR")
EndIf
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)

I create an IE, catch the HTML code of youtube.com and write it to the IE.

The first problem I have is that I have to change the address bar to the value of the url I connect to with WinHTTP.

Because, and here is my second problem, the page has to load after I used _IEDocWriteHTML but because of an url != the url in the address bar, the site couldn't load the pictures and so on.

Just that easy. In the HTML the location for a picture is "/picture/foo.jpeg"

Now the url in the address bar is about:blank so he search for "about:blank/picture/foo.jpeg" but it has to be "youtube.com/picture/foo.jpeg". So I have to change the url value in the address bar and then the HTML I wrote in the IE has to go on loading.

Possible or not?

Just replace relative to absolute path. Use for example, StringReplace() function.
Link to comment
Share on other sites

hi, how can i ignore certificat error, such as "The host name in the certificate is invalid or does not match"?

_WinHttpSetOption($hOpen,$WINHTTP_OPTION_SECURITY_FLAGS,$SECURITY_FLAG_IGNORE_CERT_CN_INVALID)

is this correct?

Edited by ohos
Link to comment
Share on other sites

  • 3 months later...

Hi all,

is anyone here who has a fritzbox (dsl router)?

I want to talk to my fritzbox by winhttp, and it works fine as long as i go through the normal webinterface (port 80).

The fritzbox can also be controlled by ssl (port 443) and i can not get this to work.

I think the problem is that the ssl certificate for the fritzbox is not signed (the browser allows me to visit the page anyway but warns).

In PHP (using curl) it works with

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_USERPWD, $this->remote_config_user . ':' . $this->remote_config_password);

Any help is welcome, thanks...

EDIT: removed bad code, working code below

Edited by Allow2010
Link to comment
Share on other sites

Thanks for the idea, but i set the port, see boold text

Regarding the http flag, where do i have to set it?

is this correct (is BitOr used to combine the flags?)

_WinHttpSetOption($hOpen, $WINHTTP_OPTION_SECURITY_FLAGS, BitOR($SECURITY_FLAG_IGNORE_CERT_CN_INVALID, $SECURITY_FLAG_IGNORE_UNKNOWN_CA))

EDIT: removed bad code, working code below

Edited by Allow2010
Link to comment
Share on other sites

After trying (and reading many MSDN pages) i found it out.

for fritzbox router, the important part is that

  • they can be used locally by http (port 80) (a password is requested if set)
  • remotely by https (port 443 or custom port)
  • if using ssl, the certificate is not valid/signed, so we have to accept bad ssl certs to make this work
  • if using ssl, also basic athentication (username and password as set in the fritzbox) have to be transmitted
  • after that the normal password is needed (not shown in this demo script)
here is the working sample

FBconnect.au3

Edited by Allow2010
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...