I'm trying to use libcurl in D to download a page that requires logging in first. At the moment though, I can't even get the logging in working. I tried with curl.exe, got it working, and used the --libcurl command to export C code that I then turned into (I think) equivalent D code.
The problem is that when I POST the login form, curl.exe gets a HTTP 200 to the correct page, whereas libcurl gets a HTTP 302 back to the login page, which is the same behaviour I noticed when cookies weren't being saved/reused.
I've tried with the HTTP struct functions, and also setting curl options explicitly like the C code does. Is there something I've missed?
import std.file;
import std.net.curl;
import std.stdio;
void main() {
getLogon();
postLogon();
getIndex();
}
void getLogon()
{
auto http = HTTP();
http.onReceive = (ubyte[] data) { /+ drop +/ std.file.write("logon1.html", data); /+writeln(cast(char[])(data)); stdout.flush;+/ return data.length; };
http.handle.set(CurlOption.tcp_nodelay, 1);
http.handle.set(CurlOption.buffersize, 102400);
http.handle.set(CurlOption.noprogress, 1);
//http.handle.set(CurlOption.useragent, "curl/7.57.0");
http.handle.set(CurlOption.maxredirs, 50);
//http.handle.set(CurlOption.cainfo, "C:\\Users\\Josh\\Downloads\\curl-7.57.0-win64-mingw\\bin\\curl-ca-bundle.crt");
http.handle.set(CurlOption.cookiejar, "cookie.dat");
http.handle.set(CurlOption.cookiefile, "cookie.dat");
http.handle.set(CurlOption.verbose, 1);
http.handle.set(CurlOption.url, "https://foo.com/logon.php");
http.method(HTTP.Method.get);
http.perform();
}
void doLogon()
{
auto http = HTTP();
http.onReceive = (ubyte[] data) { /+ drop +/ std.file.write("logon2.html", data); /+writeln(cast(char[])(data)); stdout.flush;+/ return data.length; };
http.handle.set(CurlOption.tcp_nodelay, 1);
http.handle.set(CurlOption.buffersize, 102400);
http.handle.set(CurlOption.noprogress, 1);
//http.handle.set(CurlOption.useragent, "curl/7.57.0");
http.handle.set(CurlOption.maxredirs, 50);
//http.handle.set(CurlOption.cainfo, "C:\\Users\\Josh\\Downloads\\curl-7.57.0-win64-mingw\\bin\\curl-ca-bundle.crt");
http.handle.set(CurlOption.cookiejar, "cookie.dat");
http.handle.set(CurlOption.cookiefile, "cookie.dat");
http.handle.set(CurlOption.verbose, 1);
http.handle.set(CurlOption.url, "https://foo.com/logon.php");
http.handle.set(CurlOption.postfields, "username=user&password=pass&Logon=submit");
http.handle.set(CurlOption.postfieldsize_large, 52);
http.method(HTTP.Method.post);
http.perform();
}
void getIndex()
{
auto http = HTTP();
http.onReceive = (ubyte[] data) { /+ drop +/ std.file.write("index.html", data); /+writeln(cast(char[])(data)); stdout.flush;+/ return data.length; };
http.handle.set(CurlOption.tcp_nodelay, 1);
http.handle.set(CurlOption.buffersize, 102400);
http.handle.set(CurlOption.noprogress, 1);
//http.handle.set(CurlOption.useragent, "curl/7.57.0");
http.handle.set(CurlOption.maxredirs, 50);
//http.handle.set(CurlOption.cainfo, "C:\\Users\\Josh\\Downloads\\curl-7.57.0-win64-mingw\\bin\\curl-ca-bundle.crt");
http.handle.set(CurlOption.cookiejar, "cookie.dat");
http.handle.set(CurlOption.cookiefile, "cookie.dat");
http.handle.set(CurlOption.verbose, 1);
http.handle.set(CurlOption.url, "https://foo.com/index.php");
http.method(HTTP.Method.get);
http.perform();
}
libcurl verbose dump: https://pastebin.com/Sq60CLHV
curl.exe verbose dump: https://pastebin.com/KBDDNq9k
Thanks,
Josh
|