HttpClients下載與入門,httpclients入門

來源:互聯網
上載者:User

HttpClients下載與入門,httpclients入門

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

 

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

?
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()); // 讀取狀態資訊               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()); // 讀取狀態資訊               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請求設定了請求的表單參數。


參考資料:

jquery ajax 設定json類型的返回串問題  http://www.itmmd.com/201504/694.htmljava jsp實現萬年曆 + 議程管理,已經設計好u  http://www.itmmd.com/201504/693.htmljquery 動態添加和刪除 ul li列表  http://www.itmmd.com/201504/692.html更多資訊請訪問:萌萌的it人

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.