中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

HttpClients下載與入門

2018-07-20    來源:open-open

容器云強勢上線!快速搭建集群,上萬Linux鏡像隨意使用

Http協(xié)議的重要性相信不用我多說了,HttpClient相比傳統(tǒng)JDK自帶的URLConnection,增加了易用性和靈活性(具體區(qū)別,日后我 們再討論),它不僅是客戶端發(fā)送Http請求變得容易,而且也方便了開發(fā)人員測試接口(基于Http協(xié)議的),即提高了開發(fā)的效率,也方便提高代碼的健壯 性。因此熟練掌握HttpClient是很重要的必修內容,掌握HttpClient后,相信對于Http協(xié)議的了解會更加深入。

 

你可以從官方獲取最新版本:http://hc.apache.org/index.html。也可以從這里獲取官方使用入門示例。
以下示例,輸出所有頭內容,和頁面返回的內容。


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()); // 讀取狀態(tài)信息

        

       Header[] hd = response1.getAllHeaders(); // 所有頭信息

       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>(); // 設置表單參數

   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()); // 讀取狀態(tài)信息

        

       Header[] hd = response1.getAllHeaders(); // 所有頭信息

       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();

  }

 }

  

 /**

  * 讀取流

  */

 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();

 }

} 

// 結束

GET和POST請求都輸出了獲取到的服務器內容,POST請求設置了請求的表單參數。

標簽: 代碼 服務器

版權申明:本站文章部分自網絡,如有侵權,請聯(lián)系:west999com@outlook.com
特別注意:本站所有轉載文章言論不代表本站觀點!
本站所提供的圖片等素材,版權歸原作者所有,如需使用,請與原作者聯(lián)系。

上一篇:ApachePOI 動態(tài)列表生成excel

下一篇: iOS使用代碼截圖