2011年11月1日 星期二

Java Curl

Linux

1. install curl
yum install curl curl-devle

2. Download the source code and compile
http://curl.haxx.se/libcurl/java/

3.Compile the code:


gunzip curl-java-0.2.2.tar.gz
tar xvf curl-java-0.2.2.tar
cd curl-java-0.2.2
make

windows:

1. Download the source code and compile to dll
http://curl.haxx.se/libcurl/java/
compiled dll: http://www.gknw.de/mirror/curl/curl_java/

2.Copy to system32
3. or set the VM args in eclipse
ie Window->Preferences->Java->Installed JREs. Then choose your current JRE(JDK) and click Edit. Fill Default VM Arguments: -Djava.library.path=/usr/local/xuggler/lib.
ref: StackOverflow


Java Code

try
{
// Loading up the shared JNI
System.loadLibrary("javacurl");

} catch (Exception e) {

e.printStackTrace();

}


test.java
// This test application accesses the Web site from Banco Santander, a Mexican bank, performs a login
// over SSL, receives a cookie and obtain the balance of a bank account, all using libcurl from Java

// import CurlGlue;
// import CurlWrite;

class test extends CurlIO {

public int handleString(byte s[])
{
/* output everything */
System.out.println("IIIIIIIIIII -------------- OOOOOOOOOOOOOOOOOOO");
try {

System.out.write(s);

}
catch (java.io.IOException moo) {
// nothing
}
return 0;
}

public static void main(String[] args)
{

// Bank account number and PIN - fake of course :)
String account = new String("1234567");
String pinCode = new String("1234");

CurlGlue cg;

try {
test cw = new test();

// Register callback write function
cg = new CurlGlue();
cg.setopt(CurlGlue.CURLOPT_WRITEFUNCTION, cw);

// Login to the bank's secure Web site, posting account number and PIN code
cg.setopt(CurlGlue.CURLOPT_URL, "https://www.santander.com.mx/SuperNetII/servlet/Login");
cg.setopt(CurlGlue.CURLOPT_SSLVERSION, 3);
cg.setopt(CurlGlue.CURLOPT_SSL_VERIFYPEER, 0); // attention! Insecure mode!!
cg.setopt(CurlGlue.CURLOPT_VERBOSE, 1);
cg.setopt(CurlGlue.CURLOPT_FOLLOWLOCATION, 1);
cg.setopt(CurlGlue.CURLOPT_POST, 1);
cg.setopt(CurlGlue.CURLOPT_COOKIEJAR, "cookie.txt");
cg.setopt(CurlGlue.CURLOPT_POSTFIELDS, "pag=login&pagErr=/index.html&usuario="+account+"&clave="+pinCode+"&irAmodulo=1");
cg.perform();

// Access the bank account balance re-using the session ID stored in memory inside a cookie
cg.setopt(CurlGlue.CURLOPT_URL, "https://www.santander.com.mx/SuperNetII/servlet/Cchequeras");
cg.setopt(CurlGlue.CURLOPT_SSLVERSION, 3);
cg.setopt(CurlGlue.CURLOPT_SSL_VERIFYPEER, 0); // attention! Insecure mode!!
cg.setopt(CurlGlue.CURLOPT_VERBOSE, 1);
cg.setopt(CurlGlue.CURLOPT_FOLLOWLOCATION, 1);
cg.setopt(CurlGlue.CURLOPT_COOKIEFILE, "cookie.txt");
cg.perform();

// The cookie.txt file is actually created now
cg.finalize();

} catch (Exception e) {
e.printStackTrace();
}
}
}

useful websit:
http://chimpler.blogspot.com/2009/03/logging-to-ebay-with-java-curl.html

1 則留言:

  1. http://www.gknw.de/mirror/curl/curl_java/ seems like the website is offline

    回覆刪除