 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
goldensoft
Joined: 23 Jul 2007 Posts: 11
|
Posted: Tue Sep 25, 2007 4:27 pm Post subject: Is it Possible to upload a file by HTTP Post? |
|
|
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 Thu Sep 27, 2007 8:59 am; edited 3 times in total |
|
| Back to top |
|
 |
goldensoft
Joined: 23 Jul 2007 Posts: 11
|
Posted: Wed Sep 26, 2007 8:33 am Post subject: |
|
|
| can any one help me on this? |
|
| Back to top |
|
 |
goldensoft
Joined: 23 Jul 2007 Posts: 11
|
Posted: Thu Sep 27, 2007 8:59 am Post subject: |
|
|
| Does anyone found an example that can be used? |
|
| Back to top |
|
 |
BoBoĻ Guest
|
Posted: Thu Sep 27, 2007 10:10 am Post subject: |
|
|
| 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 |
|
| Back to top |
|
 |
goldensoft
Joined: 23 Jul 2007 Posts: 11
|
Posted: Thu Sep 27, 2007 5:04 pm Post subject: |
|
|
| 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? |
|
| Back to top |
|
 |
Loriss
Joined: 26 Jul 2004 Posts: 62
|
|
| Back to top |
|
 |
adamrgolf
Joined: 28 Dec 2006 Posts: 440
|
Posted: Tue Jan 19, 2010 9:35 pm Post subject: |
|
|
| 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? |
|
| Back to top |
|
 |
kahi
Joined: 19 Jan 2010 Posts: 3
|
Posted: Wed Jan 20, 2010 8:19 am Post subject: |
|
|
| anyone? |
|
| Back to top |
|
 |
LonstaDaMonsta Guest
|
Posted: Sat Feb 20, 2010 1:41 pm Post subject: |
|
|
| I to need help on this |
|
| Back to top |
|
 |
..:: Free Radical ::..
Joined: 20 Sep 2006 Posts: 72
|
Posted: Sun Apr 11, 2010 5:28 pm Post subject: |
|
|
why do you guys need to use libcurl when httpquery can do it?
There is an imageshack example posted in that thread too. |
|
| Back to top |
|
 |
n-l-i-d Guest
|
Posted: Mon Apr 12, 2010 2:21 pm Post subject: |
|
|
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/initialization.js.php?aflID=10829822
- http://player.omroep.nl/xml/metaplayer.xml.php?aflID=10829822&md5=624bba93a664a6fe173eacdcb13f9d6b
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&kcode=bb&ratio=16:9&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&kcode=sb&ratio=16:9&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 |
|
| Back to top |
|
 |
RaptorX
Joined: 19 Feb 2010 Posts: 580
|
Posted: Mon Aug 23, 2010 9:18 pm Post subject: |
|
|
| 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. |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|