Jump to content
youtuber

winhttp Multilink problem

Recommended Posts

youtuber

I cannot make Multi connections with winhttp

Where am I making mistakes?

#include <Array.au3>
#include <String.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $ScriptDir = @ScriptDir
If StringRight($ScriptDir, 1) <> "\" Then $ScriptDir &= "\"
Global $aReadsFile = $ScriptDir & "UrlList.txt"
FileDelete($aReadsFile)
Global $aOpenFile = FileOpen($aReadsFile,1)

$Form1 = GUICreate("Form1", 1231, 584)
$Edit1 = GUICtrlCreateEdit("" & @CRLF, 16, 40, 401, 457, BitOR($ES_MULTILINE, $ES_AUTOVSCROLL, $WS_VSCROLL))
GUICtrlSetData(-1, "")
$Edit2 = GUICtrlCreateEdit("", 432, 40, 240, 457)
GUICtrlSetData(-1, "")
$ButtonUrlCheck = GUICtrlCreateButton("Button URL Check", 16, 520, 107, 25)
$CheckData = GUICtrlCreateButton("Check Data", 16, 552, 107, 25)
$Edit3 = GUICtrlCreateEdit("" & @CRLF, 680, 40, 529, 457, BitOR($ES_MULTILINE, $ES_AUTOVSCROLL, $WS_VSCROLL))
GUICtrlSetData(-1, "")
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $ButtonUrlCheck
            $aGetNewComment = StringRegExp(MultipleConnections("https://www.autoitscript.com/forum/"), '<a\s*href=[\"](.*?)\?do\=getNewComment[\"].*?>',3)
For $c = 0 To UBound($aGetNewComment) -1
FileWrite($aOpenFile, $aGetNewComment[$c] & @CRLF)
Next
$aOpenFile2 = FileOpen(@ScriptDir & '\UrlList.txt', 0)
GUICtrlSetData($Edit1, FileRead($aOpenFile2))
FileClose($aOpenFile)

        Case $CheckData
            $aEditRead = StringSplit(StringStripCR(GUICtrlRead($Edit1)), @LF,2)
For $i = 0 To UBound($aEditRead) -1
$aGetConnectURL = MultipleConnections($aEditRead[$i])
GUICtrlSetData($Edit3, $aGetConnectURL & @CRLF,1)
Next

$aEditRead3 = StringSplit(StringStripCR(GUICtrlRead($Edit3)), @LF,2)
$GetTheTopics = _StringBetween($aEditRead3, 'data-controller="core.front.core.lightboxedImages">','</div>')
For $c = 0 To UBound($GetTheTopics) -1
    GUICtrlSetData($Edit2, $GetTheTopics[$i] & @CRLF ,1)
Next

EndSwitch
WEnd

Func MultipleConnections($address)
    Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
    $oMyError = ObjEvent("AutoIt.Error", "httperror")
    $oHTTP.Open("GET", $address, False)
    $oHTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0")
    $oHTTP.SetRequestHeader("Accept", "*/*")
    $oHTTP.SetRequestHeader("Accept-Language", "tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3")
    $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded")
    $oHTTP.SetRequestHeader("X-Requested-With", "XMLHttpRequest")
    $oHTTP.SetRequestHeader("Referer", "https://www.autoitscript.com/forum/")
    $oHTTP.SetRequestHeader("Content-Length", "34")
    $oHTTP.SetRequestHeader("Connection", "keep-alive")
    $oHTTP.Send()
    If @error Then
ConsoleWrite("Error connection")
Else
     Local $sReceived = $oHTTP.ResponseText
EndIf
    If $oHTTP.Status = 200 Then
        $oHTTP = Null
        Return $sReceived
    EndIf
    $oHTTP = Null
    Return -1
EndFunc

Func httperror()
    ConsoleWrite("http error" & @CRLF)
EndFunc

 

Share this post


Link to post
Share on other sites
ripdad
Func MultipleConnections($address); <-- Function can only make ONE SINGLE connection at a time.

    $oMyError = ObjEvent("AutoIt.Error", "httperror"); <-- this line needs to be near the top under #includes.

    Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1")
    $oHTTP.Open("GET", $address, False)
    $oHTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:55.0) Gecko/20100101 Firefox/55.0")
    $oHTTP.SetRequestHeader("Accept", "*/*")
    $oHTTP.SetRequestHeader("Accept-Language", "tr-TR,tr;q=0.8,en-US;q=0.5,en;q=0.3")

    ; These are not needed for making a REQUEST
    ; -----------------------------------------
    ;$oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded"); <- not sending anything
    ;$oHTTP.SetRequestHeader("X-Requested-With", "XMLHttpRequest"); <- doubt this would help at all
    ;$oHTTP.SetRequestHeader("Referer", "https://www.autoitscript.com/forum/"); <- no need
    ;$oHTTP.SetRequestHeader("Content-Length", "34"); <- one length for all? not needed here
    ;$oHTTP.SetRequestHeader("Connection", "keep-alive"); <- not needed

    $oHTTP.Send()
    If @error Then
        ConsoleWrite("Error connection")
        $oHTTP = 0

        Return SetError(1); <-- triggers an error. (code above function needs error checking)

    EndIf

    If $oHTTP.Status = 200 Then
        Local $sReceived = $oHTTP.ResponseText
        $oHTTP = Null; <-- use 0 instead
        Return $sReceived
    EndIf

    $oHTTP = Null; <-- use 0 instead
    Return -1
EndFunc

Func httperror()
    ConsoleWrite("http error" & @CRLF)

    $oMyError.Clear; <-- clears the error

    Return SetError(1); <-- triggers an error
EndFunc

 

  • Like 1

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
youtuber

There is a problem I can not get data from here

Case $CheckData
            $aEditRead = StringSplit(StringStripCR(GUICtrlRead($Edit1)), @LF,2)
For $i = 0 To UBound($aEditRead) -1
$aGetConnectURL = MultipleConnections($aEditRead[$i])
GUICtrlSetData($Edit3, $aGetConnectURL & @CRLF,1)
Next

$aEditRead3 = StringSplit(StringStripCR(GUICtrlRead($Edit3)), @LF,2)
$GetTheTopics = _StringBetween($aEditRead3, 'data-controller="core.front.core.lightboxedImages">','</div>')
For $c = 0 To UBound($GetTheTopics) -1
    GUICtrlSetData($Edit2, $GetTheTopics[$i] & @CRLF ,1) ;<--- The problem line
Next

 

Share this post


Link to post
Share on other sites
ripdad

You will have to provide UrlList.txt --
at least 2 or 3 url's in it.


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
ripdad

Okay.

I cannot do it right now. I have an appointment to go to.
I will be back in about 2 hours.

In the meantime, if someone else is available to help,
that would be okay.

Otherwise, I will help when I get back.

  • Like 1

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
ripdad

I'm back.

Not really sure what you want to accomplish. Do you want what members say in each post, or something else?

Can you explain the outcome you expect? Be specific.

---

See anything wrong in these 3 lines?

$c and $i ?

For $c = 0 To UBound($GetTheTopics) -1
    GUICtrlSetData($Edit2, $GetTheTopics[$i] & @CRLF ,1)
Next

 


"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
ripdad

That's because the source code of the webpage is broken with StringSplit().

So, it's fragmented and out of place for _StringBetween(), which is StringRegExp().

You cannot use StringSplit(). It will break the source every time.

In addition, you might have to strip some characters from the source.

$string = $strSourceCode

$string = StringRegExpReplace($string, '(?s)[\r\n\t\v]', '')

and then, strip the whitespace.

$string = StringStripWS($string, 7)

Then use _StringBetween().

---

You didn't answer my first and second question in post #9.

 

  • Like 1

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
youtuber

reply post #9

Data from $ Edit3

view-source:https://www.autoitscript.com/forum/topic/190621-winhttp-multilink-problem/
data-controller="core.front.core.lightboxedImages"> to get the source from  </div>

 

Share this post


Link to post
Share on other sites
youtuber

@ripdad I'm not exactly sure how?

$aEditRead3 = GUICtrlRead($Edit3)

$StringR = StringRegExpReplace($aEditRead3, '(?s)[\r\n\t\v]', '')
$StringWS = StringStripWS($StringR, 7)
$GetTheTopics = _StringBetween($StringWS, 'data-controller="core.front.core.lightboxedImages">','</div>')

For $c = 0 To UBound($GetTheTopics) -1
    GUICtrlSetData($Edit2, $GetTheTopics[$c] & @CRLF ,1)
Next

 

 

Share this post


Link to post
Share on other sites
ripdad

I will be busy for the next hour. Will be back after that.

  • Like 1

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
ripdad

This should work without the need for $Edit3...

 

Case $CheckData
            Local $array = StringSplit(GUICtrlRead($Edit1), @CRLF, 1)
            Local $aData, $string

            For $i = 1 To $array[0]
                $string = MultipleConnections($array[$i])
                If @error Then
                    GUICtrlSetData($Edit2, 'Connection Error' & @CRLF, 1)
                    ContinueLoop
                EndIf

                $string = StringRegExpReplace($string, '(?s)[\n\r\t\v]', '')
                $string = StringStripWS($string, 7)
                $aData = _StringBetween($string, 'data-controller="core.front.core.lightboxedImages">', '</div>')

                For $j = 0 To UBound($aData) - 1
                    GUICtrlSetData($Edit2, $aData[$j] & @CRLF, 1)
                Next
            Next
    EndSwitch

 

If you do use $Edit3, you will have to increase its character limit and do one source at a time. You cannot add html sources on top of one another. It will cause you grief.

$Edit3 = GUICtrlCreateEdit(..)

GUICtrlSetLimit($Edit3, 500000)

 

  • Like 1

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

Share this post


Link to post
Share on other sites
youtuber

Thank you for better information now I understand you

I have another question :)

Global  $oMyError = ObjEvent("AutoIt.Error", "httperror"); <-- this line needs to be near the top under #includes.

Is it more logical to add this line?
global on top

#include <Array.au3>
#include <String.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $ScriptDir = @ScriptDir
If StringRight($ScriptDir, 1) <> "\" Then $ScriptDir &= "\"
Global $aReadsFile = $ScriptDir & "UrlList.txt"
FileDelete($aReadsFile)
Global $aOpenFile = FileOpen($aReadsFile,1)

Global  $oMyError = ObjEvent("AutoIt.Error", "httperror"); <-- this line needs to be near the top under #includes.

$Form1 = GUICreate("Form1", 1231, 584)

 

Share this post


Link to post
Share on other sites
ripdad

This would be a cleaner solution...

 

#include <Array.au3>
#include <String.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Global $oMyError = ObjEvent('AutoIt.Error', 'httperror')

Global $sFile = @ScriptDir & '\UrlList.txt'
Global $hFile = FileOpen($sFile, 2)
Global $Form1 = GUICreate('Form1', 1231, 584)

 

Opt('MustDeclareVars', 1) ensures that you declare all variables. It's good practice to use it.

 

--edit--

corrected an error on second parameter of FileOpen()

 

 

Edited by ripdad
  • Like 1

"The mediocre teacher tells. The Good teacher explains. The superior teacher demonstrates. The great teacher inspires." -William Arthur Ward

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

×