HttpClients download and getting started, httpclients getting started

Source: Internet
Author: User

HttpClients download and getting started, httpclients getting started

I don't need to say much about the importance of the Http protocol. Compared with the URLConnection provided by the traditional JDK, HttpClient adds ease of use and flexibility (the specific difference will be discussed later ), it not only makes it easy for the client to send Http requests, but also facilitates the developer to test the interface (based on the Http protocol), which improves the development efficiency and code robustness. Therefore, it is very important to master HttpClient. After you master HttpClient, I believe that you will have a better understanding of the Http protocol.

 

You can get the latest official version: http://hc.apache.org/index.html. You can also obtain the official getting started example from here.
The following example outputs all the header content and the content returned by the page.

?
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 import java.io.ByteArrayOutputStream;import java.io.InputStream;import java.util.ArrayList;import java.util.List;import org.apache.http.Header;import org.apache.http.HttpEntity;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.apache.http.message.BasicNameValuePair;public class QuickStart { public static void main(String[] args) {  try {   CloseableHttpClient httpclient = HttpClients.createDefault();   HttpGet httpGet = new HttpGet("http://javacui.com");   CloseableHttpResponse response1 = httpclient.execute(httpGet);   try {       System.out.println(response1.getStatusLine()); // Read status information               Header[] hd = response1.getAllHeaders(); // All header information       for(Header h : hd){        System.out.println(h.getName() + ":" + h.getValue());       }               HttpEntity entity1 = response1.getEntity();       System.out.println(EntityUtils.toString(entity1));   finally {       response1.close();   }   HttpPost httpPost = new HttpPost("http://javacui.com");   List <NameValuePair> paras = new ArrayList <NameValuePair>(); // Set form parameters   paras.add(new BasicNameValuePair("username""name"));   paras.add(new BasicNameValuePair("password""pass"));   httpPost.setEntity(new UrlEncodedFormEntity(paras));   CloseableHttpResponse response2 = httpclient.execute(httpPost);   try {    System.out.println(response1.getStatusLine()); // Read status information               Header[] hd = response1.getAllHeaders(); // All header information       for(Header h : hd){        System.out.println(h.getName() + ":" + h.getValue());       }               HttpEntity entity1 = response1.getEntity();       System.out.println(EntityUtils.toString(entity1));   finally {       response2.close();   }  catch (Exception e) {   e.printStackTrace();  } }   /**  * Stream reading  */ public static byte[] readStream(InputStream inStream) throws Exception {  ByteArrayOutputStream outSteam = new ByteArrayOutputStream();  byte[] buffer = new byte[1024];  int len = -1;  while ((len = inStream.read(buffer)) != -1) {   outSteam.write(buffer, 0, len);  }  outSteam.close();  inStream.close();  return outSteam.toByteArray(); }// End

The GET and POST requests both output the obtained server content, and the post request sets the form parameters of the request.


References:

Jquery ajax sets json type return string problem http://www.itmmd.com/201504/694.htmljava jsp realize Perpetual calendar + schedule management, u http://www.itmmd.com/201504/693.htmljquery has been designed to dynamically add and delete ul li list http://www.itmmd.com/201504/692.htmlmore information please visit: Meng it people

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.