AutoHotkey Community

It is currently May 25th, 2012, 4:39 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: September 25th, 2007, 5:27 pm 
Offline

Joined: July 23rd, 2007, 11:20 pm
Posts: 11
Does any one know how to to use libcurl.dll (http://curl.haxx.se/libcurl/)to upload a file through http post to a web form?

I know this is possible to perform with curl.exe (http://curl.haxx.se/) but I'm looking for a more professional way without requiring external programs. I searched in the forum and it seems that this has not yet been developed libcurl.dll

there are a few examples on how to use curl.exe:

- http://www.autohotkey.com/forum/topic22779.html
- http://www.autohotkey.com/forum/topic21822.html


I found a good example on how to download a file but not on how to upload

http://www.autohotkey.com/forum/topic22621.html


I think this could be a great addiction to ahk users.


Last edited by goldensoft on September 27th, 2007, 9:59 am, edited 3 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 26th, 2007, 9:33 am 
Offline

Joined: July 23rd, 2007, 11:20 pm
Posts: 11
can any one help me on this?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 27th, 2007, 9:59 am 
Offline

Joined: July 23rd, 2007, 11:20 pm
Posts: 11
Does anyone found an example that can be used?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 27th, 2007, 11:10 am 
Quote:
Does anyone found an example that can be used?
More interessting - why didn't you find an example that can be used. :?

Search the forum for:
libcurl - 7 hits
dllcall(libcurl - 3 hits


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: September 27th, 2007, 6:04 pm 
Offline

Joined: July 23rd, 2007, 11:20 pm
Posts: 11
Quote:
More interessting - why didn't you find an example that can be used.


I have already tried to search for libcurl examples but I only found an example on how to download a file not on how to upload it to a webform via http post.

Does anyone knows how to do this?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 28th, 2007, 4:27 pm 
Offline

Joined: July 26th, 2004, 9:07 pm
Posts: 62
You have to ask to (olfen) on http://www.autohotkey.com/forum/topic22621.html

_________________
Loriss


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 19th, 2010, 10:35 pm 
Offline

Joined: December 28th, 2006, 9:46 am
Posts: 440
goldensoft wrote:

I have already tried to search for libcurl examples but I only found an example on how to download a file not on how to upload it to a webform via http post.

Does anyone knows how to do this?

I've got a similar question, here.

Can anyone help us figure this out?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 20th, 2010, 9:19 am 
Offline

Joined: January 19th, 2010, 2:15 am
Posts: 3
anyone?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 20th, 2010, 2:41 pm 
I to need help on this


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: April 11th, 2010, 6:28 pm 
Offline

Joined: September 20th, 2006, 2:05 pm
Posts: 73
why do you guys need to use libcurl when httpquery can do it?

There is an imageshack example posted in that thread too.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 12th, 2010, 3:21 pm 
1 - You get libcurl.dll, curl.exe and the AHK library from DeathByNukes.
2 - You consult the documentation for curl.exe.
3 - You create a command-line with curl.exe that works, and add "--libcurl <file>" as option to let it output the code to a file.
4 - You open the created file and have a look at the necessary calls to CurlEasySetOption.
5 - You adapt the example code to include the CurlEasySetOption settings.

This is an example output of the "--libcurl <file>" option:

Quote:
/********* Sample code generated by the curl command line tool **********
* Lines with [REMARK] below might need to be modified to make this code
* usable. Add error code checking where appropriate.
* Compile this with a suitable header include path. Then link with
* libcurl.
* If you use any *_LARGE options, make sure your compiler figure
* out the correct size for the curl_off_t variable.
* Read the details for all curl_easy_setopt() options online on:
* http://curlm.haxx.se/libcurl/c/curl_easy_setopt.html
************************************************************************/
#include <curl/curl.h>

int main(int argc, char *argv[])
{
CURLcode ret;
CURL *hnd = curl_easy_init();
/* curl_easy_setopt(hnd, CURLOPT_WRITEDATA, 0x22fee8); [REMARK] */
/* curl_easy_setopt(hnd, CURLOPT_WRITEFUNCTION, 0x4056a4); [REMARK] */
/* curl_easy_setopt(hnd, CURLOPT_READDATA, 0x22ff2c); [REMARK] */
/* curl_easy_setopt(hnd, CURLOPT_READFUNCTION, 0x40563c); [REMARK] */
/* curl_easy_setopt(hnd, CURLOPT_SEEKDATA, 0x22ff2c); [REMARK] */
/* curl_easy_setopt(hnd, CURLOPT_SEEKFUNCTION, 0x404fe0); [REMARK] */
curl_easy_setopt(hnd, CURLOPT_INFILESIZE_LARGE, (curl_off_t)-1);
curl_easy_setopt(hnd, CURLOPT_URL, "http://www.uitzendinggemist.nl/");
curl_easy_setopt(hnd, CURLOPT_PROXY, NULL);
curl_easy_setopt(hnd, CURLOPT_NOPROGRESS, 0);
curl_easy_setopt(hnd, CURLOPT_HEADER, 0);
curl_easy_setopt(hnd, CURLOPT_FAILONERROR, 0);
curl_easy_setopt(hnd, CURLOPT_UPLOAD, 0);
curl_easy_setopt(hnd, CURLOPT_DIRLISTONLY, 0);
curl_easy_setopt(hnd, CURLOPT_APPEND, 0);
curl_easy_setopt(hnd, CURLOPT_NETRC, 0);
curl_easy_setopt(hnd, CURLOPT_FOLLOWLOCATION, 0);
curl_easy_setopt(hnd, CURLOPT_UNRESTRICTED_AUTH, 0);
curl_easy_setopt(hnd, CURLOPT_TRANSFERTEXT, 0);
curl_easy_setopt(hnd, CURLOPT_USERPWD, NULL);
curl_easy_setopt(hnd, CURLOPT_PROXYUSERPWD, NULL);
curl_easy_setopt(hnd, CURLOPT_NOPROXY, NULL);
curl_easy_setopt(hnd, CURLOPT_RANGE, NULL);
/* curl_easy_setopt(hnd, CURLOPT_ERRORBUFFER, 0x22fd98); [REMARK] */
curl_easy_setopt(hnd, CURLOPT_TIMEOUT, 0);
curl_easy_setopt(hnd, CURLOPT_REFERER, NULL);
curl_easy_setopt(hnd, CURLOPT_AUTOREFERER, 0);
curl_easy_setopt(hnd, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0;)");
curl_easy_setopt(hnd, CURLOPT_FTPPORT, NULL);
curl_easy_setopt(hnd, CURLOPT_LOW_SPEED_LIMIT, 0);
curl_easy_setopt(hnd, CURLOPT_LOW_SPEED_TIME, 0);
curl_easy_setopt(hnd, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t)0);
curl_easy_setopt(hnd, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t)0);
curl_easy_setopt(hnd, CURLOPT_RESUME_FROM_LARGE, (curl_off_t)0);
curl_easy_setopt(hnd, CURLOPT_COOKIE, NULL);
curl_easy_setopt(hnd, CURLOPT_HTTPHEADER, NULL);
curl_easy_setopt(hnd, CURLOPT_SSLCERT, NULL);
curl_easy_setopt(hnd, CURLOPT_SSLCERTTYPE, NULL);
curl_easy_setopt(hnd, CURLOPT_SSLKEY, NULL);
curl_easy_setopt(hnd, CURLOPT_SSLKEYTYPE, NULL);
curl_easy_setopt(hnd, CURLOPT_KEYPASSWD, NULL);
curl_easy_setopt(hnd, CURLOPT_SSH_PRIVATE_KEYFILE, NULL);
curl_easy_setopt(hnd, CURLOPT_SSH_PUBLIC_KEYFILE, NULL);
curl_easy_setopt(hnd, CURLOPT_SSH_HOST_PUBLIC_KEY_MD5, NULL);
curl_easy_setopt(hnd, CURLOPT_SSL_VERIFYHOST, 2);
curl_easy_setopt(hnd, CURLOPT_SSH_KNOWNHOSTS, "C:\Documents and Settings\User\Application Data/_ssh/known_hosts");
curl_easy_setopt(hnd, CURLOPT_MAXREDIRS, 50);
curl_easy_setopt(hnd, CURLOPT_CRLF, 0);
curl_easy_setopt(hnd, CURLOPT_QUOTE, NULL);
curl_easy_setopt(hnd, CURLOPT_POSTQUOTE, NULL);
curl_easy_setopt(hnd, CURLOPT_PREQUOTE, NULL);
curl_easy_setopt(hnd, CURLOPT_WRITEHEADER, NULL);
curl_easy_setopt(hnd, CURLOPT_COOKIEFILE, NULL);
curl_easy_setopt(hnd, CURLOPT_COOKIEJAR, "F:\Downloads\100302\curl_DLL_ONLY\nossl\testcooo.txt");
curl_easy_setopt(hnd, CURLOPT_COOKIESESSION, 0);
curl_easy_setopt(hnd, CURLOPT_SSLVERSION, 0);
curl_easy_setopt(hnd, CURLOPT_TIMECONDITION, 0);
curl_easy_setopt(hnd, CURLOPT_TIMEVALUE, 0);
curl_easy_setopt(hnd, CURLOPT_CUSTOMREQUEST, NULL);
/* curl_easy_setopt(hnd, CURLOPT_STDERR, 0x77c5fcc0); [REMARK] */
curl_easy_setopt(hnd, CURLOPT_HTTPPROXYTUNNEL, 0);
curl_easy_setopt(hnd, CURLOPT_INTERFACE, NULL);
curl_easy_setopt(hnd, CURLOPT_KRBLEVEL, NULL);
curl_easy_setopt(hnd, CURLOPT_TELNETOPTIONS, NULL);
curl_easy_setopt(hnd, CURLOPT_RANDOM_FILE, NULL);
curl_easy_setopt(hnd, CURLOPT_EGDSOCKET, NULL);
curl_easy_setopt(hnd, CURLOPT_CONNECTTIMEOUT, 0);
curl_easy_setopt(hnd, CURLOPT_ENCODING, NULL);
curl_easy_setopt(hnd, CURLOPT_FTP_CREATE_MISSING_DIRS, 0);
curl_easy_setopt(hnd, CURLOPT_IPRESOLVE, 0);
curl_easy_setopt(hnd, CURLOPT_FTP_ACCOUNT, NULL);
curl_easy_setopt(hnd, CURLOPT_IGNORE_CONTENT_LENGTH, 0);
curl_easy_setopt(hnd, CURLOPT_FTP_SKIP_PASV_IP, 0);
curl_easy_setopt(hnd, CURLOPT_FTP_FILEMETHOD, 0);
curl_easy_setopt(hnd, CURLOPT_FTP_ALTERNATIVE_TO_USER, NULL);
curl_easy_setopt(hnd, CURLOPT_SSL_SESSIONID_CACHE, 1);
/* curl_easy_setopt(hnd, CURLOPT_SOCKOPTFUNCTION, 0x405018); [REMARK] */
/* curl_easy_setopt(hnd, CURLOPT_SOCKOPTDATA, 0x22f8b8); [REMARK] */
curl_easy_setopt(hnd, CURLOPT_POSTREDIR, 0);
ret = curl_easy_perform(hnd);
curl_easy_cleanup(hnd);
return (int)ret;
}
/**** End of sample code ****/


Example with cookies:

http://www.uitzendinggemist.nl/ is the Dutch public broadcasting site. To be able to view video, one has to go to the site. This annoyed me, because I wanted to be able to get the video stream address from any particular video, so I could play it back in VLC, without having to consult my firewall or some other sniffing app.

So, here comes curl/libcurl to the resque.

1 - I installed Fiddler to be able to monitor the traffic.
2 - I went to the site http://www.uitzendinggemist.nl/, next I clicked on a player link (in the form http://player.omroep.nl/?aflID=10829822) and waited until the video started playing, stopped the monitoring and analyzed the calls made.
3 - I found out that the site connects to the following urls:

- http://www.uitzendinggemist.nl/
- http://player.omroep.nl/?aflID=10829822
- http://player.omroep.nl/js/initializati ... D=10829822
- http://player.omroep.nl/xml/metaplayer. ... dcb13f9d6b

The third link retreives a .js file with the following in the first line: "var securityCode = '624bba93a664a6fe173eacdcb13f9d6b';var useFactor = true;"

This securityCode is what I need to be able to call the fourth link.

This is the resulting code for curl.exe:

Code:
; --- Link 1

url1 =
(C
http://www.uitzendinggemist.nl/
)

cmdline1 =
(LTrim C Join%A_Space%
curl.exe
"%url1%"
-A "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0;)"
-c "%A_ScriptDir%\ugcookies.txt"
-o "%A_ScriptDir%\ug1.htm"

)

RunWait, %cmdline1%, %A_ScriptDir%
If ErrorLevel
  msgbox % errorlevel "`n`n" url1

; --- Link 2

url2 =
(C
http://player.omroep.nl/?aflID=10829822
)

cmdline2 =
(LTrim C Join%A_Space%
curl.exe
-L "%url2%"
-A "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0;)"
-b "%A_ScriptDir%\ugcookies.txt"
-c "%A_ScriptDir%\ugcookies.txt"
-o "%A_ScriptDir%\ug2.htm"

)

RunWait, %cmdline2%, %A_ScriptDir%
If ErrorLevel
  msgbox % errorlevel "`n`n" url2

; --- Link 3

url3 =
(C
http://player.omroep.nl/js/initialization.js.php?aflID=10829822
)

cmdline3 =
(LTrim C Join%A_Space%
curl.exe
-L "%url3%"
-A "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0;)"
-b "%A_ScriptDir%\ugcookies.txt"
-c "%A_ScriptDir%\ugcookies.txt"
-o "%A_ScriptDir%\ug3.htm"

)

RunWait, %cmdline3%, %A_ScriptDir%
If ErrorLevel
  msgbox % errorlevel "`n`n" url3

; --- Parse result

FileReadLine, LineNr1, ug3.htm, 1

; var securityCode = '624bba93a664a6fe173eacdcb13f9d6b';var useFactor = true;

If !(RegExMatch(LineNr1, "securityCode = '(.*)'", secCode))
{
  MsgBox No security code found.
  ExitApp
}

; --- Link 4

url4 =
(C
http://player.omroep.nl/xml/metaplayer.xml.php?aflID=10829822&md5=%secCode1%
)

cmdline4 =
(LTrim C Join%A_Space%
curl.exe
-L "%url4%"
-A "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0;)"
-b "%A_ScriptDir%\ugcookies.txt"
-c "%A_ScriptDir%\ugcookies.txt"
-o "%A_ScriptDir%\ug4.htm"

)

RunWait, %cmdline4%, %A_ScriptDir%
If ErrorLevel
  msgbox % errorlevel "`n`n" url4



This results in the last file (xml) containing the stream links I need:

Quote:
<streams aspect_ratio='16:9'>
<stream compressie_kwaliteit='bb' compressie_formaat='wmv'>http://cgi.omroep.nl/legacy/player?/ceres/1/nos/serie/10Jnl22n2maza/POW_00261469/bb.20100409.asf</stream>
<stream compressie_kwaliteit='bb' compressie_formaat='sl'>episodeID=10829822</stream>
<stream compressie_kwaliteit='bb' compressie_formaat='mov'>/stream/?fcode=mov&amp;kcode=bb&amp;ratio=16:9&amp;streamurl=http%3A%2F%2Fcgi.omroep.nl%2Flegacy%2Fplayer%3F%2Fceres%2F1%2Fnos%2Fserie%2F10Jnl22n2maza%2FPOW_00261469%2Fbb.20100409.m4v</stream>
<stream compressie_kwaliteit='sb' compressie_formaat='wmv'>http://cgi.omroep.nl/legacy/player?/ceres/1/nos/serie/10Jnl22n2maza/POW_00261469/sb.20100409.asf</stream>
<stream compressie_kwaliteit='sb' compressie_formaat='mov'>/stream/?fcode=mov&amp;kcode=sb&amp;ratio=16:9&amp;streamurl=http%3A%2F%2Fcgi.omroep.nl%2Flegacy%2Fplayer%3F%2Fceres%2F1%2Fnos%2Fserie%2F10Jnl22n2maza%2FPOW_00261469%2Fsb.20100409.m4v</stream>
</streams>



Now the same with libcurl.dll:

Code:
#SingleInstance, force
#NoEnv
#include FileHelper.ahk ; http://www.autohotkey.com/forum/topic19608.html
#include libcurl.ahk

if ( CurlGlobalInit( "libcurl" ) != 0 )
   ExitApp
   
hCurlEasy := CurlEasyInit()
CurlEasyDefineOptions()
CurlGetInfoDefine()

CurlShowErrors()

; --- General

cookieDir := A_ScriptDir . "\cookies\"
cookieName := "www.uitzendinggemist.nl.txt"

; delete old temp files
IfExist % cookieDir . cookieName
  FileDelete % cookieDir . cookieName

Loop 4
{
  IfExist % "ug" . A_Index . ".html"
    FileDelete % "ug" . A_Index . ".html"
}

userAgent := "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; en-US)"

pCurlWriteFunction := RegisterCallback("CurlWriteFunction", "C F")
CurlEasySetOption( hCurlEasy, CURLOPT_WRITEFUNCTION, pCurlWriteFunction )

; --- Link 1

Url = http://www.uitzendinggemist.nl/
hFile := CreateFile( A_ScriptDir . "\ug1.html", 2 )

CurlEasySetOption( hCurlEasy, CURLOPT_URL, &URL )
VarSetCapacity(CURL_ERROR, CURL_ERROR_SIZE + 1)
CurlEasySetOption( hCurlEasy, CURLOPT_ERRORBUFFER, &CURL_ERROR )
CurlEasySetOption(hCurlEasy, CURLOPT_USERAGENT, userAgent)

CurlEasySetOption(hCurlEasy, CURLOPT_COOKIEJAR, cookieDir . cookieName)

If ( CurlEasyPerform( hCurlEasy ) )
  MsgBox % CURL_ERROR

CloseHandle(hFile)

;CurlEasyCleanup( hCurlEasy )


; --- Link 2

Url = http://player.omroep.nl/?aflID=10829822
hFile := CreateFile( A_ScriptDir . "\ug2.html", 2 )

CurlEasySetOption( hCurlEasy, CURLOPT_WRITEFUNCTION, pCurlWriteFunction )

CurlEasySetOption( hCurlEasy, CURLOPT_URL, &URL )
VarSetCapacity(CURL_ERROR, CURL_ERROR_SIZE + 1)
CurlEasySetOption( hCurlEasy, CURLOPT_ERRORBUFFER, &CURL_ERROR )
CurlEasySetOption(hCurlEasy, CURLOPT_USERAGENT, userAgent)

CurlEasySetOption(hCurlEasy, CURLOPT_COOKIEFILE, cookieDir . cookieName)
CurlEasySetOption(hCurlEasy, CURLOPT_COOKIEJAR, cookieDir . cookieName)

If ( CurlEasyPerform( hCurlEasy ) )
  MsgBox % CURL_ERROR

CloseHandle(hFile)

;CurlEasyCleanup( hCurlEasy )


; --- Link 3

Url = http://player.omroep.nl/js/initialization.js.php?aflID=10829822
hFile := CreateFile( A_ScriptDir . "\ug3.html", 2 )

CurlEasySetOption( hCurlEasy, CURLOPT_WRITEFUNCTION, pCurlWriteFunction )

CurlEasySetOption( hCurlEasy, CURLOPT_URL, &URL )
VarSetCapacity(CURL_ERROR, CURL_ERROR_SIZE + 1)
CurlEasySetOption( hCurlEasy, CURLOPT_ERRORBUFFER, &CURL_ERROR )
CurlEasySetOption(hCurlEasy, CURLOPT_USERAGENT, userAgent)

CurlEasySetOption(hCurlEasy, CURLOPT_COOKIEFILE, cookieDir . cookieName)
CurlEasySetOption(hCurlEasy, CURLOPT_COOKIEJAR, cookieDir . cookieName)

If ( CurlEasyPerform( hCurlEasy ) )
  MsgBox % CURL_ERROR

CloseHandle(hFile)

;CurlEasyCleanup( hCurlEasy )


; --- Parse result

FileReadLine, LineNr1, ug3.html, 1

; var securityCode = '624bba93a664a6fe173eacdcb13f9d6b';var useFactor = true;

If !(RegExMatch(LineNr1, "securityCode = '(.*)'", secCode))
{
  MsgBox No security code found.
  ExitApp
}

; --- Link 4

Url = http://player.omroep.nl/xml/metaplayer.xml.php?aflID=10829822&md5=%secCode1%
hFile := CreateFile( A_ScriptDir . "\ug4.html", 2 )

CurlEasySetOption( hCurlEasy, CURLOPT_WRITEFUNCTION, pCurlWriteFunction )

CurlEasySetOption( hCurlEasy, CURLOPT_URL, &URL )
VarSetCapacity(CURL_ERROR, CURL_ERROR_SIZE + 1)
CurlEasySetOption( hCurlEasy, CURLOPT_ERRORBUFFER, &CURL_ERROR )
CurlEasySetOption(hCurlEasy, CURLOPT_USERAGENT, userAgent)

CurlEasySetOption(hCurlEasy, CURLOPT_COOKIEFILE, cookieDir . cookieName)
CurlEasySetOption(hCurlEasy, CURLOPT_COOKIEJAR, cookieDir . cookieName)

If ( CurlEasyPerform( hCurlEasy ) )
  MsgBox % CURL_ERROR

CloseHandle(hFile)



; --- Cleanup and Exit

CurlEasyCleanup( hCurlEasy )

CurlFreeLibrary()

ExitApp

Return


; write to file function
CurlWriteFunction(pBuffer, size, nitems, pOutStream)
{
   global hFile
   WriteFile(hFile, pBuffer, size*nitems)
   return size*nitems
}


To use curl/libcurl to submit forms, check the documentation (-d/--data <data> and/or --data-urlencode <data> and/or -F/--form <name=content> and/or --form-string <name=string>)

HTH


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: August 23rd, 2010, 10:18 pm 
Offline

Joined: February 19th, 2010, 8:07 pm
Posts: 610
n-l-i-d wrote:
power post here


This is the best answer i have seen, like in any forum i have read before... kudos for the patience.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], engunneer, MSN [Bot], Yahoo [Bot] and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group