About the Curl Java analog HTTP request

Source: Internet
Author: User

Recently the author of the project needs Java to simulate HTTP requests, get DNS resolution TCP connections and other detailed time-consuming information.

The urlconnection provided in the Java API and the httpclient provided by Apache are not up to this requirement, and two development is too time consuming. So Google's.

Finally, two solutions are obtained:

One is to use http4j.

The Open source project uses the socket method, simulates the request, records the timestamp, and basically satisfies the requirement. For the header customization and other details, on this basis can be more convenient two times development. Only some of these bugs need to be fixed, such as when HTTPS links are not getting SSL time. The risk of using the project is instability and controllability.


Http4j code after a minor change.

http://download.csdn.net/detail/zhongyuan_1990/8837281


The second is to use curl.

Google's, curl itself has no support for Java, and a third provides a binding to use curl. May be the author's limited ability to successfully compile it in Windows. Google also did not find resources to download the relevant javacurl.dll. Finally had to give up. Select Use the command line mode to manipulate curl.

Java using the Curl command demo

Package Com.netbirdtech.libcurl.test;import Java.io.bufferedreader;import Java.io.ioexception;import Java.io.inputstreamreader;public class Test {public static void main (string[] args) {String []CMDS = {"Curl", "-I", "-w", "Status%{http_code};dns Time%{time_namelookup};" + "Wait Time%{time_pretransfer}tcp connection%{time_connect}; Make request%{time_ Starttransfer}; "+" Total Time%{time_total} "," http://www.baidu.com "}; Processbuilder pb=new Processbuilder (CMDS);p B.redirecterrorstream (true); Process p;try {p = Pb.start (); BufferedReader Br=null; String line=null;br=new BufferedReader (New InputStreamReader (P.getinputstream ())); while ((Line=br.readline ())! = NULL) {System.out.println ("\ t" +line);} Br.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

About the Curl Java analog HTTP request

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.