Jump to content

Improved _INetGetSource


w0uter
 Share

Recommended Posts

i would like this tested before i submit it to some dev.

[edit]

made the buffer from 3 to 256

[/edit]

[edit2]

removed :whistle:

[/edit2]

[edit3]

stripping leftover @CR & @LF at the end

[/edit3]

[edit4]

instead of looping i just trim the last 2 chars to get rid of the leftover enter

reduced filesize a bit

removed edit 2 since it does support ftp://

[/edit4]

[edit5]

added support for Headers & changed user-agent to "AutoIt v3"

[/edit5]

Func _INetGetSource($s_URL, $s_Header = '')
    Local $h_DLL = DllOpen("wininet.dll"), $ai_IO, $ai_IOU, $ai_ICH, $ai_IRF[5] = [0, 0, '', 0, 1], $s_Buf = ''
    $ai_IO = DllCall($h_DLL, 'int', 'InternetOpen', 'str', "AutoIt v3", 'int', 0, 'str', '', 'str', '', 'int', 0)
    If @error Or $ai_IO[0] = 0 Then
        DllClose($h_DLL)
        SetError(1)
        Return 0
    EndIf
    $ai_IOU = DllCall($h_DLL, 'int', 'InternetOpenUrl', 'int', $ai_IO[0], 'str', $s_URL, 'str', $s_Header, 'int', StringLen($s_Header), 'int', 0x80000000, 'int', 0)
    If @error Or $ai_IOU[0] = 0 Then
        DllCall($h_DLL, 'int', 'InternetCloseHandle', 'int', $ai_IO[0])
        DllClose($h_DLL)
        SetError(1)
        Return 0
    EndIf
    Local $v_Struct = DllStructCreate ('udword')
    While $ai_IRF[4] <> 0
        $s_Buf &= StringMid($ai_IRF[2], 1, $ai_IRF[4])
        $ai_IRF = DllCall($h_DLL, 'int', 'InternetReadFile', 'int', $ai_IOU[0], 'str', "", 'int', 256, 'ptr', DllStructGetPtr ($v_Struct, 1))
        $ai_IRF[4] = DllStructGetData ($v_Struct, 1)
    WEnd
    DllStructDelete ($v_Struct)
    DllCall($h_DLL, 'int', 'InternetCloseHandle', 'int', $ai_IOU[0])
    DllCall($h_DLL, 'int', 'InternetCloseHandle', 'int', $ai_IO[0])
    DllClose($h_DLL)
    Return StringTrimRight($s_Buf, 1)
EndFunc;==>_INetGetSource

ConsoleWrite(_INetGetSource('http://www.autoitscript.com') & @LF)

to make cameronsdad happy:

(related to InternetReadFile)

LPVOID had nothing to do with it, just using 'str' was good.

the problem was with "LPDWORD lpdwNumberOfBytesRead" i first just used a 0 and now i use Dllstruct (im sure there is a cleaner way :dance:)

Edited by w0uter

My UDF's:;mem stuff_Mem;ftp stuff_FTP ( OLD );inet stuff_INetGetSource ( OLD )_INetGetImage _INetBrowse ( Collection )_EncodeUrl_NetStat_Google;random stuff_iPixelSearch_DiceRoll

Link to comment
Share on other sites

w0uter,

I just curious why you say this is better/more control than COM?

Using COM you can get the same result in 4 lines of code:

Func _INetGetSource($s_URL)
    $WinHttpReq = ObjCreate("WinHttp.WinHttpRequest.5.1")
    $WinHttpReq.Open("GET", $s_URL, false)
    $WinHttpReq.Send()
    Return $WinHttpReq.ResponseText
EndFunc

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

to make cameronsdad happy:

(related to InternetReadFile)

LPVOID had nothing to do with it, just using 'str' was good.

the problem was with "LPDWORD lpdwNumberOfBytesRead" i first just used a 0 and now i use Dllstruct (im sure there is a cleaner way :whistle:)

<{POST_SNAPBACK}>

wow, i almost never look in this forum (mostly just support because thinking on scripts helps pass time at work) and i can't tell you how surprised i was to see my name. Thanks for remembering my request, and yes, it did in fact make me happy.

***edit*** realized i'd quoted all of your code etc and didn't think that was necessary for the small amount i was actually replying to, so i removed it...

Edited by cameronsdad
Link to comment
Share on other sites

  • 2 weeks later...

Func _INetGetSource($s_URL)
    $WinHttpReq = ObjCreate("WinHttp.WinHttpRequest.5.1")
    $WinHttpReq.Open("GET", $s_URL, false)
    $WinHttpReq.Send()
    Return $WinHttpReq.ResponseText
EndFunc

<{POST_SNAPBACK}>

COM + non existing URL = total crash.

<{POST_SNAPBACK}>

wOuter, I like your code.

For learning purposes, I'd like to know if there is a way to use the WinHttpRequest.onerror event to trap a failure and use Dale's shorter code?

My UDFs: ExitCodes

Link to comment
Share on other sites

  • 2 weeks later...
  • 2 weeks later...
  • Developers

see edit 5.

i would also like this to replace the current UDF in the beta.

Could you do me a favor and PM or mail me any updates to UDF's (func or docs) because there is always a good chance I am missing posts like this .. tnx ;)

Got this one done ...... so no need to sent that.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • 3 weeks later...

wOuter, I like your code.

For learning purposes, I'd like to know if there is a way to use the WinHttpRequest.onerror event to trap a failure and use Dale's shorter code?

WoUter I also like your code I use this in one.

$s_URL = InputBox("WebPage", "Type WebPage To Get Source From")

_INetGetSource($s_URL)

Func _INetGetSource($s_URL)
    $WinHttpReq = ObjCreate("WinHttp.WinHttpRequest.5.1")
    $WinHttpReq.Open("GET", $s_URL, false)
    $WinHttpReq.Send()
    ClipPut($WinHttpReq.ResponseText)
    Run("Notepad.exe")
    WinWaitActive("Untitled - Notepad")
    Send("{CTRLDOWN}v{CTRLUP}")
EndFunc
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...