Jump to content

retry request with new proxy WINHTTP


Recommended Posts

#include "WinHttp.au3"
#include <array.au3>
#include <file.au3>
#include <WinAPI.au3>
#include <string.au3>
#include <WindowsConstants.au3>
#include <FileConstants.au3>
#include <ListviewConstants.au3>
#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

; Learning HTTP Requests with WinHTTP ;

Global $idOP, $usr, $list, $idEXIT, $call
HotKeySet("{ESC}", "Terminate")

Local $hGUI = GUICreate("Learning", 750, 500, 223, 202, $WS_BORDER)
Local $idEXIT = GUICtrlCreateButton("Exit", 660, 440, 75, Default)
Local $idCon = GUICtrlCreateButton("Start", 10, 10, 725, Default)
Local $siteLab = GUICtrlCreateLabel("Select Test", 20, 48 ,Default, 35)
GUICtrlSetFont (-1,9, 800); bold
Local $IG = GUICtrlCreateCheckbox("test", 75, 45)
Local $realm = GUICtrlCreateCheckbox("test1", 75, 65)
Local $site3 = GUICtrlCreateCheckbox("test2", 200, 45)
Local $site4 = GUICtrlCreateCheckbox("test3", 200, 65)

$list = GUICtrlCreateListView( "     PROXY     |     NAME1     |     NAME2     |     STATUS     ", 20, 100, 710, 300, $LVS_NOSORTHEADER+$LVS_SINGLESEL)


GUISetState(@SW_SHOW, $hGUI)

While 1

        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE, $idEXIT
                Exit
            Case $IG
                If _IsChecked($IG) Then
                    $call = "test"
                EndIf
            Case $idCon
                Call($call)

                    EndSwitch
    WEnd

Func test()

$url = "www.w3schools.com"
$sPage = "/tags/demo_form.asp"
Local $proxies
Local $names
_FileReadToArray("proxies.txt", $proxies) ; read the list of names to array
_FileReadToArray("Okay.txt", $names) ; read the list of names to array
For $i = 1 To UBound($proxies) - 1
$Read = $names[$i]
$Datastring = ('')
$newreadamount = _StringBetween($read,$Datastring, ':')
$newreadamount[0] = StringReplace($newreadamount[0], ":", "")
$name1 = $newreadamount[0]
$Datastring2 = (':')
$newreadamount2 = _StringBetween($read,$Datastring2, '')
$newreadamount2[0] = StringReplace($newreadamount2[0], ":", "")
$name2 = $newreadamount2[0]

$sAdditionalData = "name1="&$name1&"&name2="&$name2

        MsgBox(4096, "Test", $proxies[$i] & " - " & $name1&":"&$name2,1)

; Initialize and get session handle

$hOpen = _WinHttpOpen("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.0", $WINHTTP_ACCESS_TYPE_NAMED_PROXY, $proxies[$i])
_WinHttpSetTimeouts($hOpen, 15, 15, 15, 15)
; Get connection handle

$hConnect = _WinHttpConnect($hOpen, $url)

; Make a request
$hRequest = _WinHttpOpenRequest($hConnect, "POST", $sPage)

; Send it. Specify additional data to send too. This is required by the Google API:
_WinHttpSendRequest($hRequest, "Content-Type: application/x-www-form-urlencoded", $sAdditionalData)

; Wait for the response
_WinHttpReceiveResponse($hRequest)

; See what's returned
Dim $sReturned
If _WinHttpQueryDataAvailable($hRequest) Then ; if there is data
    Do
        $sReturned &= _WinHttpReadData($hRequest)
    Until @error
EndIf
; Close handles
_WinHttpCloseHandle($hRequest)
_WinHttpCloseHandle($hConnect)
_WinHttpCloseHandle($hOpen)
; See what's returned
MsgBox(4096, "Returned", $sReturned, 1)

If StringInStr($sReturned,'Input was received as:') Then
    GUICtrlCreateListViewItem($proxies[$i] & "|"&$name1&"|"&$name2&"|Success", $list)
    Sleep(500)
    ContinueLoop
ElseIf $sReturned = "" Then
    FileWrite("nottested.txt",$name1&":"&$name2 & @CRLF)
    GUICtrlCreateListViewItem($proxies[$i] & "|"&$name1&"|"&$name2&"|Bad Proxy", $list)
Sleep(500)
ContinueLoop
EndIf

Next

EndFunc

Func Terminate()
    Exit 0
EndFunc

Func _IsChecked($idControlID)
    Return BitAND(GUICtrlRead($idControlID), $GUI_CHECKED) = $GUI_CHECKED
EndFunc   ;==>_IsChecked

This is my code and it runs just fine.
My problem is that if I receive Bad Proxy I need it to test the request again with the same array input $name1 / $name2 until I receive
'Input was received as:'
So basically
 

ElseIf $sReturned = "" Then
    FileWrite("nottested.txt",$name1&":"&$name2 & @CRLF)
    GUICtrlCreateListViewItem($proxies[$i] & "|"&$name1&"|"&$name2&"|Bad Proxy", $list)
Sleep(500)
TRY AGAIN WITH NEW PROXY AND SAME CREDENTIALS
EndIf

Is this possible and if so do you have either some example code and/or some helpfile I can read.

Thanks in advance!

Link to comment
Share on other sites

Hello. Just create a function where you can check a return value.

 

so you can  do: Do until (my condition is OK)

 

Saludos

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

×
×
  • Create New...