當前位置:編程學習大全網 - 源碼下載 - 如何使用HttpClient

如何使用HttpClient

壹、簡介

HttpClient是Apache Jakarta Common下的子項目,用來提供高效的、最新的、功能豐富的支持HTTP協議的客戶端編程工具包,並且它支持HTTP協議最新的版本和建議。HttpClient已經應用在很多的項目中,比如Apache Jakarta上很著名的另外兩個開源項目Cactus和HTMLUnit都使用了HttpClient。

二、特性

1. 基於標準、純凈的java語言。實現了Http1.0和Http1.1

2. 以可擴展的面向對象的結構實現了Http全部的方法(GET, POST, PUT, DELETE, HEAD, OPTIONS, and TRACE)。

3. 支持HTTPS協議。

4. 通過Http代理建立透明的連接。

5. 利用CONNECT方法通過Http代理建立隧道的.test;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import java.security.KeyManagementException;

import java.security.KeyStore;

import java.security.KeyStoreException;

import java.security.NoSuchAlgorithmException;

import java.security.cert.CertificateException;

import java.util.ArrayList;

import java.util.List;

import javax.net.ssl.SSLContext;

import org.apache.ment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);

HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("bin", bin).addPart("comment", comment).build();

httppost.setEntity(reqEntity);

System.out.println("executing request " + httppost.getRequestLine());

CloseableHttpResponse response = httpclient.execute(httppost);

try {

System.out.println("----------------------------------------");

System.out.println(response.getStatusLine());

HttpEntity resEntity = response.getEntity();

if (resEntity != null) {

System.out.println("Response content length: " + resEntity.getContentLength());

}

EntityUtils.consume(resEntity);

} finally {

response.close();

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

httpclient.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}</namevaluepair></namevaluepair></namevaluepair></namevaluepair>

  • 上一篇:Java:如何把字符串數據a轉換成int數據65?
  • 下一篇:關於閉包 vue3 防抖 節流
  • copyright 2024編程學習大全網