Jump to content

WinHTTP functions


trancexx
 Share

Recommended Posts

I did that. 2 days ago.

Global $hRequest = _WinHttpOpenRequest($hConnect2, "POST", "/takeupload.php", Default, "/upload.php", "*/*")
Global $boundary = Random(1000000000000, 99999999999999, 1)
Global $sData = ""
$sData &= '-----------------------------' & $boundary & @CRLF
$sData &= 'Content-Disposition: form-data; name="MAX_FILE_SIZE"' & @CRLF & @CRLF
$sData &= '1000000' & @CRLF
$sData &= '-----------------------------' & $boundary & @CRLF
$sData &= 'Content-Disposition: form-data; name="type"' & @CRLF & @CRLF
$sData &= "25" & @CRLF
$sData &= '-----------------------------' & $boundary & @CRLF
$sData &= 'Content-Disposition: form-data; name="file"; filename="1.torrent"' & @CRLF
$sData &= 'Content-Type: application/x-bittorrent' & @CRLF & @CRLF
$sData &= $fileread & @CRLF
;~ $sData &= FileRead(@DesktopDir & "\raw2.txt") & @CRLF ;~ $sData &= _Base64Encode(FileRead("C:UsersLykerDesktopOtherUntitled-1.png")) & @CRLF
$sData &= '-----------------------------' & $boundary & @CRLF
$sData &= 'Content-Disposition: form-data; name="name"' & @CRLF & @CRLF
$sData &= 'imenatorenta' & @CRLF
$sData &= '-----------------------------' & $boundary & @CRLF
$sData &= 'Content-Disposition: form-data; name="nfo"; filename="1.txt"' & @CRLF
$sData &= 'Content-Type: text/plain' & @CRLF & @CRLF
$sData &= 'nfoto' & @CRLF
$sData &= '-----------------------------' & $boundary & @CRLF
$sData &= 'Content-Disposition: form-data; name="descr"' & @CRLF & @CRLF
$sData &= 'opis' & @CRLF
$sData &= '-----------------------------' & $boundary & @CRLF
$sData &= 'Content-Disposition: form-data; name="remLen"' & @CRLF & @CRLF
$sData &= '14996' & @CRLF
$sData &= '-----------------------------' & $boundary & @CRLF
$sData &= 'Content-Disposition: form-data; name="youtube"' & @CRLF & @CRLF
$sData &= '' & @CRLF
$sData &= '-----------------------------' & $boundary & @CRLF
$sData &= 'Content-Disposition: form-data; name="google"' & @CRLF & @CRLF
$sData &= '' & @CRLF
$sData &= '-----------------------------' & $boundary & "--"
;~ ConsoleWrite($sData)
$datasize = StringLen($sData)
_WinHttpSendRequest($hRequest, "", $sData)
_WinHttpReceiveResponse($hRequest)

$sResult = _WinHttpReadData($hRequest)
ClipPut($sResult)
ConsoleWrite($sResult & @CRLF)

_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect2)
_WinHttpCloseHandle($hOpen2)

There are 2 additional heards

Content-Length 15365 Content-Type multipart/form-data; boundary=---------------------------71902182822295

Which i'm not sure how to send.. _WinHttpSendRequest($hRequest, ADDITIONAL HEARDS HERE?, $sData) ?

Link to comment
Share on other sites

"Content-Length 15whatever5" is automatically calculated and sent by winhttp (it does that for me, at least)

"Content-Type multipart/form-data; boundary=---------------------------71902182822295" - this one you MUST send yourself in order for this to work at all

Probably the best way to do this is to use 2nd argument of _WinHttpSendRequest():

_WinHttpSendRequest( $hRequest, 'Content-Type: multipart/form-data; boundary=---------------------------' & $boundary & @CRLF , $sData )

Please notice that there are TWO LESS dashes.

I tested it - it works!

Edit: By the way, random() can output a maximum of 2147483648 -- but you can do this:

Random(100000000, 999999999, 1) & Random(100000000, 999999999, 1) & Random(100000000, 999999999, 1)

Edit2: Also just stumbled over a server that did not accept my query unless I included linefeed at the end of data like this:

$sData &= '-----------------------------' & $boundary & "--" & @CRLF ; <<<<<<<<<<<<<<<<<<<< added CRLF
Edited by Shambler0
Link to comment
Share on other sites

Um, trancexx, could you please tell me how to get any meaningful error codes out of winhttp functions?

Define meaningful.

If you get error then you can call _WinAPI_GetLastError() to get extended information about the error. If the error is the API specific, GetLastError will return one of WinHTTP error codes defined inside WinHttpConstants.au3.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Ok. How should I edit the script so that it'll send "cat=25" to upload.php?

I don't see anything terribly wrong with the code you already posted. I even tested it on my server and it worked just fine.

Maybe you are doing something else wrong. Without seeing actual page it's impossible to tell for me.

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Ok, it seems that GetLastError() actually works in most cases.

In my case ("POST" method, server does not reply) GetLastError() just returns zeroes, and then suddenly it starts returning ERROR_WINHTTP_INCORRECT_HANDLE_STATE

I assumed I should call something other than GetLastError(), but have just looked at MSDN examples - they call GetLastError()

So the question should be asked on MSDN instead of here. Sorry to bother you.

Link to comment
Share on other sites

  • 2 weeks later...

Hi,

first, let me thank you for this UDF, it helped me a lot in my projects.

But today i came across a big problem using the UDF on Windows 8 64bit (did not test 32bit)

I wrote this code

$soResult = _WinHttpSetOption($hOpen, BitOR($SECURITY_FLAG_IGNORE_UNKNOWN_CA, $SECURITY_FLAG_IGNORE_CERT_CN_INVALID))
            If @error Or $soResult = 0 Then
                MsgBox(0,"","Error: _WinHttpSetOption failed with code " & @error)
            EndIf

to access a site (router) that uses SSL but does not have a valid certificate.

It all works just fine when run on Windows 7 64bit, but fails when run on Windows 8 64bit.

The Error it gives me is error 4 (DLLCall failed).

I tried setting other Options (like the useragent from the helpfile) and it works fine...

Seems to be just a problem with $WINHTTP_OPTION_SECURITY_FLAGS on Win8.

Can onyone help me, i am totally lost here and this breaks my code...i have not found a way around this problem yet...

EDIT: FYI: runnig latest Autoit public BETA 3.3.9.4 and compiling in 32bit

EDIT2: fixed bad copy and paste of the code...pasted a test code by mistake

Edited by Allow2010
Link to comment
Share on other sites

$aCall returns the following (see picture)

maybe someone knows what the values mean...

 

I also took the Win7 winhttp.dll and renamed it to winhttp5.dll and put it in my system32 folder in my Windows 8 System.

Then i changed

Global Const $hWINHTTPDLL__WINHTTP = DllOpen("winhttp5.dll")
DllOpen("winhttp5.dll") ; making sure reference count never reaches 0

in Winhttp.au3 and it works fine...

So there has to be an incompatibillity with the Win8 winhttp version...maybe win8 made it harder to ignore certificates in an attempt to be more secure...

I searched the net for changes in Winhttp but i could not find any useful documentation...

post-5062-0-24137900-1368652692_thumb.jp

Edited by Allow2010
Link to comment
Share on other sites

^^ Is there possibility that you are doing something wrong?

For example, what's the 1000 value that you set? Maybe you meant SECURITY_FLAG_IGNORE_CERT_CN_INVALID? That constant is declared inside WinHttpConstants.au3 and has value of 0x00001000. That's whole different value than the one you use.

Next, why are you setting it for internet session? Try for specific request, usually named $hRequest.

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Thanks for your reply...

the 1000 was just a bad copy and paste from my tries (i tried different values to see if i get different returncodes, but it is always 4)

I replaced the code in the post above to prevent further irritation.

the real code is

$allow_unknownCA_wrongCN = BitOR($SECURITY_FLAG_IGNORE_UNKNOWN_CA, $SECURITY_FLAG_IGNORE_CERT_CN_INVALID)
            $soResult = _WinHttpSetOption($hOpen, $WINHTTP_OPTION_SECURITY_FLAGS, $allow_unknownCA_wrongCN)
            If @error Or $soResult = 0 Then
                _FB_Logfile("Error: _WinHttpSetOption failed with code " & @error)
                Return 0
            EndIf

the codes is needed to connect to a router which uses a self signed certificate.

It works just fin on Win7 and not in Win 8 (but works fine again when i use the win7 dll on win 8 as explained above)

i am 100% certain that i am not doing anything wrong. You can see the complete project here:

Next, why are you setting it for internet session? Try for specific request, usually named $hRequest.

 

well that might be a good point. I did set it for the session as it is used globally throughout the complete connection and i only need to set it once. It works just fine on win7...

I will try to set it for the request an will report back as soon as i can find the time...thank you again...

Edited by Allow2010
Link to comment
Share on other sites

Yes you were right, thank you:

on Win7 it is possible to do this

  $soResult = _WinHttpSetOption($hOpen, BitOR($SECURITY_FLAG_IGNORE_UNKNOWN_CA, $SECURITY_FLAG_IGNORE_CERT_CN_INVALID))

on Win8 it will fail.

when i do it this way, it works on Win8

$soResult = _WinHttpSetOption($hRequest, $WINHTTP_OPTION_SECURITY_FLAGS,BitOR($SECURITY_FLAG_IGNORE_UNKNOWN_CA, $SECURITY_FLAG_IGNORE_CERT_CN_INVALID))

so you tip helped my solve the issue, thank you!

Link to comment
Share on other sites

The details about $pBuffer in _WinHttpReadData are missing from the documentation.

Edited by guinness

UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to comment
Share on other sites

  • 3 weeks later...

Hi trancexxx, if you've got a webpage with

<input type="radio" value="no" name="test1">
<input type="radio" value="no" name="test2">

and want to send their value as "yes", how do you do that? I've read (the Remarks section) that it's removed from the _WinHttpSimpleFormFill function . I added "radio" to this line

$aInput = StringRegExp($sForm, "(?si)<\h*(?:input|textarea|label|fieldset|legend|select|optgroup|option|button)\h*(.*?)/*\h*>", 3)

but it won't change the value from no to yes.

Edited by hiho
Link to comment
Share on other sites

Hi trancexxx, if you've got a webpage with

<input type="radio" value="no" name="test1">
<input type="radio" value="no" name="test2">

and want to send their value as "yes", how do you do that? I've read (the Remarks section) that it's removed from the _WinHttpSimpleFormFill function . I added "radio" to this line

$aInput = StringRegExp($sForm, "(?si)<\h*(?:input|textarea|label|fieldset|legend|select|optgroup|option|button)\h*(.*?)/*\h*>", 3)

but it won't change the value from no to yes.

You can't do that. The value of radio is preset. This is how browsers do it also. If you click the radio button the value is set to "value".

Still, I need to tweak the code because in your case both radios should made into the submitted data considering they have different "name" fields.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Ok, thanks for the info. Can you give me an example /even if it is temporary fix/ how to change the value? I've read how the data is beign sent, but couldn't figure a way to do what I'm trying to accomplish.

Edited by hiho
Link to comment
Share on other sites

Ok, thanks for the info. Can you give me an example /even if it is temporary fix/ how to change the value? I've read how the data is beign sent, but couldn't figure a way to do what I'm trying to accomplish.

Change the form yourself. Read source, StringReplace() what you want and then fill altered form. Form-fill function can be fed with page source as string directly. Read help file remark about that.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

That's intentional. That parameter is for internal use or for use by advanced members.

 

...what of course made me curious :)... and led to this: You are strongly discouraged to use WinHTTP in asynchronous mode with AutoIt.

dont_push_button.jpg

Edited by KaFu
Link to comment
Share on other sites

...what of course made me curious :)... and led to this: You are strongly discouraged to use WinHTTP in asynchronous mode with AutoIt.

Hahahah, that made me laugh.

mmm, das verboten parameter..

forbidden-donut.gif

Link to comment
Share on other sites

Hi trancexx, could you tell me what am I doing wrong? This will send a request but considering the fact that the radio buttons aren't processed with the new values, I'm thinking that something ain't right. 

#include <Winhttp.au3>
Global $hOpen, $hConnect, $sRead, $id = 12345
Global $bad[2], $good[2]
$bad[0] = "name=test value=yes>"
$bad[1] = "name=test checked value=no>"
$good[0] = "name=test checked value=yes>"
$good[1] = "name=test value=no>"
$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, "www.site.com")
StringReplace($hConnect, $bad[0], $good[0])
StringReplace($hConnect, $bad[1], $good[1])
$sRead = _WinHttpSimpleFormFill($hConnect, "/edit.php?id=" & $id, "index:0", _
        ;~ my stuff here
If @error Then MsgBox(64, "", "Error code :" & @error)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
Edited by hiho
Link to comment
Share on other sites

Hi trancexx, could you tell me what am I doing wrong? This will send a request but considering the fact that the radio buttons aren't processed with the new values, I'm thinking that something ain't right. 

#include <Winhttp.au3>
Global $hOpen, $hConnect, $sRead, $id = 12345
Global $bad[2], $good[2]
$bad[0] = "name=test value=yes>"
$bad[1] = "name=test checked value=no>"
$good[0] = "name=test checked value=yes>"
$good[1] = "name=test value=no>"
$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, "www.site.com")
StringReplace($hConnect, $bad[0], $good[0])
StringReplace($hConnect, $bad[1], $good[1])
$sRead = _WinHttpSimpleFormFill($hConnect, "/edit.php?id=" & $id, "index:0", _
        ;~ my stuff here
If @error Then MsgBox(64, "", "Error code :" & @error)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
 

It should be more like this:

#include <Winhttp.au3>

Global $hOpen, $hConnect, $sRead, $id = 12345
Global $bad[2], $good[2]
$bad[0] = "name=test value=yes>"
$bad[1] = "name=test checked value=no>"
$good[0] = "name=test checked value=yes>"
$good[1] = "name=test value=no>"
$hOpen = _WinHttpOpen()
$hConnect = _WinHttpConnect($hOpen, "www.site.com")

Local $sHTML = _WinHttpSimpleRequest($hConnect, Default, "/edit.php?id=" & $id, Default, Default, "Accept: text/html;q=0.9,text/plain;q=0.8,*/*;q=0.5")
; Now you replace your shit
$sHTML = StringReplace($sHTML, $bad[0], $good[0])
$sHTML = StringReplace($sHTML, $bad[1], $good[1])
; And "action" must be full URL
$sHTML = StringReplace($sHTML, "action=whatever_is_here", "action=ful_url_of_whatever_was_there")
Local $hConnNew = $sHTML

$sRead = _WinHttpSimpleFormFill($hConnNew, $hOpen, "index:0", _
        ;~ my stuff here
If @error Then MsgBox(64, "", "Error code :" & @error)
_WinHttpCloseHandle($hConnNew)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
Edited by trancexx

♡♡♡

.

eMyvnE

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...