纯 Java CURL 实现
java-curl is a pure-java HTTP utility implemented based on HttpURLConnection in the standard JRE, while the usage references to the commonly-used CURL command line tool under Linux.
CUrl.opt(...) method. For a list of supported switches, please refer to the table.opt() method accepts multiple parameters and values. Note that if a CUrl switch needing a value, then the switch and value should be passed-in as two method parameters, e.g.: curl.opt("-d", "a=b", "-L") CUrl.IO is the abstracted interface for both input and output, its subclasses include: CUrl.MemIO: corresponds to a byte buffer for direct memory access CUrl.FileIO: corresponding to physical file CUrl.WrappedIO: Simple wrapper for either InputStream or OutputStreamcert(io, password): Read the client certificate from IO data(io, binary): read POST data from IO form(name, io): Read file/text item from IO to submit an multi-part post for file-uploading cookie(io): Read cookies from IO cookieJar(io): Save cookies to IO dumpHeader(io): dumps the response header to IO stdout/stderr: redirect standard-output/standard-error to IOcurl("http://...").opt("-D", "-", "-c", "-") CookieManager/CookieStore, but there is a serious problem, the API design is not reasonable, CookieStore can only have one globally singleton instance. That means in one VM, if multiple requests access the same site concurrently, then they always share the same cookies, this is not acceptable in many circumstances.ThreadLocal-based CookieStore, each thread has a separate cookie-store, which solves the above problem perfectly.--cookie/--cookie-jar parameter, you can also use getCookieStore to get the CookieStore singleton, directly call its add/getCookies and other methods to read and write the current thread's cookies.--cookie/--cookie-jar parameter every time.cookie("") call on the first request in the thread, which will clear the thread-local cookie-store.CUrl.Resolver is used to directly deserialize the raw response byte array into custom Java object, such as Xml, Json, Html, etc., can be combined with DOM4J, Jackson/Gson, Jsoup and other third-party libraries.Resolver.resolve() method, if CUrl.Recoverable or its subclass instances are thrown, then this fail can be retried. If retry parameters are specified, CUrl will automatically retry the given number of times or given durationRecoverable to instruct CUrl to retry.cert(io, password) or opt("-E", "path/to/file\:password").insecure() or opt("-k") to instruct CUrl to ignore certificate security checks.location() or opt("-L") to indicate that the redirect should be automatically followed.CUrl.getLocations().get(0) to get the redirected location URL after getting the 30X response.data() call demonstrations that --data switch can be specified multiple times, and the parameter values can be overwritten. public void httpPost() {
CUrl curl = new CUrl("http://httpbin.org/post")
.data("hello=world&foo=bar")
.data("foo=overwrite");
curl.exec();
assertEquals(200, curl.getHttpCode());
} public void insecureHttpsViaFiddler() {
CUrl curl = new CUrl("https://httpbin.org/get")
.proxy("127.0.0.1", 8888) // Use Fiddler to capture & parse HTTPS traffic
.insecure(); // Ignore certificate check since it's issued by Fiddler
curl.exec();
assertEquals(200, curl.getHttpCode());
} public void uploadMultipleFiles() {
CUrl.MemIO inMemFile = new CUrl.MemIO();
try { inMemFile.getOutputStream().write("text file content blabla...".getBytes()); } catch (Exception ignored) {}
CUrl curl = new CUrl("http://httpbin.org/post")
.form("formItem", "value") // a plain form item
.form("file", inMemFile) // in-memory "file"
.form("image", new CUrl.FileIO("D:\\tmp\\a2.png")); // A file in storage
curl.exec();
assertEquals(200, curl.getHttpCode());
}header(), or multiple request headers at a time with headers().………java -jar java-curl-1.2.2.jar https://httpbin.org/get ^
-x 127.0.0.1:8888 -k ^
-A "Mozilla/5.0 (Linux; U; Android 8.0.0; zh-cn; KNT-AL10 Build/HUAWEIKNT-AL10) AppleWebKit/537.36 (KHTML, like Gecko) MQQBrowser/7.3 Chrome/37.0.0.0 Mobile Safari/537.36" ^
-H "Referer: http://somesite.com/fake_referer" ^
-H "X-Requested-With: XMLHttpRequest" ^
-H "X-Auth-Token: xxxxxxx"…| Switch Name | Short-cut Method | Description |
|---|---|---|
| -E, --cert | cert | <certificate:password> Specify the client certificate file and password, only support JKS format certificate (only BKS is supported on Android) |
暂无开放 Issues,或尚未同步最近议题。