其实httpclient.jar的问题并不复杂,但是又很多的朋友都不太了解http服务器,因此呢,今天小编就来为大家分享httpclient.jar的一些知识,希望可以帮助到大家,下面我们一起来看看这个问题的分析吧!

httpclient是哪个jar

httpclient对应的jar是Apache HttpClient。

httpclient.jar http服务器

详细解释:

Apache HttpClient是一个开源的HTTP客户端库,用于Java应用程序中的网络通信。它通过方便的API允许开发者轻松与服务器交互。此库能够支持多种网络协议和通信方法,广泛用于客户端和服务端应用程序之间的数据传输。这个库并不是孤立的jar文件,而是一个由多个模块组成的项目,但通常可以通过Maven或Gradle等构建工具作为依赖项添加到项目中。此外,它还包括多种功能,如连接管理、认证方案以及更高级的功能,如流式传输、编码与解码处理等。其HTTP/HTTPS请求实现得到广泛应用和信任,支持开发者高效且安全地进行网络请求与响应交互。

Java中的httpclient4.5应该怎么使用

一、所需要的jar包

httpclient-4.5.jar

httpcore-4.4.1.jar

httpclient.jar http服务器

httpmime-4.5.jar

二、实例

Java代码

package cn.tzz.apache.httpclient;

import java.io.File;

import java.io.IOException;

import java.net.URL;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import org.apache.http.HttpEntity;

import org.apache.http.NameValuePair;

import org.apache.http.client.config.RequestConfig;

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.conn.ssl.DefaultHostnameVerifier;

import org.apache.http.conn.util.PublicSuffixMatcher;

import org.apache.http.conn.util.PublicSuffixMatcherLoader;

import org.apache.http.entity.ContentType;

import org.apache.http.entity.StringEntity;

import org.apache.http.entity.mime.MultipartEntityBuilder;

import org.apache.http.entity.mime.content.FileBody;

import org.apache.http.entity.mime.content.StringBody;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

import org.apache.http.util.EntityUtils;

public class HttpClientUtil{

private RequestConfig requestConfig= RequestConfig.custom()

.setSocketTimeout(15000)

.setConnectTimeout(15000)

.setConnectionRequestTimeout(15000)

.build();

private static HttpClientUtil instance= null;

private HttpClientUtil(){}

public static HttpClientUtil getInstance(){

if(instance== null){

instance= new HttpClientUtil();

}

return instance;

}

/**

*发送 post请求

*@param httpUrl地址

*/

public String sendHttpPost(String httpUrl){

HttpPost httpPost= new HttpPost(httpUrl);//创建httpPost

return sendHttpPost(httpPost);

}

/**

*发送 post请求

*@param httpUrl地址

*@param params参数(格式:key1=value1&key2=value2)

*/

public String sendHttpPost(String httpUrl, String params){

HttpPost httpPost= new HttpPost(httpUrl);//创建httpPost

try{

//设置参数

StringEntity stringEntity= new StringEntity(params,"UTF-8");

stringEntity.setContentType("application/x-www-form-urlencoded");

httpPost.setEntity(stringEntity);

} catch(Exception e){

e.printStackTrace();

}

return sendHttpPost(httpPost);

}

/**

*发送 post请求

*@param httpUrl地址

*@param maps参数

*/

public String sendHttpPost(String httpUrl, Map<String, String> maps){

HttpPost httpPost= new HttpPost(httpUrl);//创建httpPost

//创建参数队列

List<NameValuePair> nameValuePairs= new ArrayList<NameValuePair>();

for(String key: maps.keySet()){

nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));

}

try{

httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs,"UTF-8"));

} catch(Exception e){

e.printStackTrace();

}

return sendHttpPost(httpPost);

}

/**

*发送 post请求(带文件)

*@param httpUrl地址

*@param maps参数

*@param fileLists附件

*/

public String sendHttpPost(String httpUrl, Map<String, String> maps, List<File> fileLists){

HttpPost httpPost= new HttpPost(httpUrl);//创建httpPost

MultipartEntityBuilder meBuilder= MultipartEntityBuilder.create();

for(String key: maps.keySet()){

meBuilder.addPart(key, new StringBody(maps.get(key), ContentType.TEXT_PLAIN));

}

for(File file: fileLists){

FileBody fileBody= new FileBody(file);

meBuilder.addPart("files", fileBody);

}

HttpEntity reqEntity= meBuilder.build();

httpPost.setEntity(reqEntity);

return sendHttpPost(httpPost);

}

/**

*发送Post请求

*@param httpPost

*@return

*/

private String sendHttpPost(HttpPost httpPost){

CloseableHttpClient httpClient= null;

CloseableHttpResponse response= null;

HttpEntity entity= null;

String responseContent= null;

try{

//创建默认的httpClient实例.

httpClient= HttpClients.createDefault();

httpPost.setConfig(requestConfig);

//执行请求

response= httpClient.execute(httpPost);

entity= response.getEntity();

responseContent= EntityUtils.toString(entity,"UTF-8");

} catch(Exception e){

e.printStackTrace();

} finally{

try{

//关闭连接,释放资源

if(response!= null){

response.close();

}

if(httpClient!= null){

httpClient.close();

}

} catch(IOException e){

e.printStackTrace();

}

}

return responseContent;

}

/**

*发送 get请求

*@param httpUrl

*/

public String sendHttpGet(String httpUrl){

HttpGet httpGet= new HttpGet(httpUrl);//创建get请求

return sendHttpGet(httpGet);

}

/**

*发送 get请求Https

*@param httpUrl

*/

public String sendHttpsGet(String httpUrl){

HttpGet httpGet= new HttpGet(httpUrl);//创建get请求

return sendHttpsGet(httpGet);

}

/**

*发送Get请求

*@param httpPost

*@return

*/

private String sendHttpGet(HttpGet httpGet){

CloseableHttpClient httpClient= null;

CloseableHttpResponse response= null;

HttpEntity entity= null;

String responseContent= null;

try{

//创建默认的httpClient实例.

httpClient= HttpClients.createDefault();

httpGet.setConfig(requestConfig);

//执行请求

response= httpClient.execute(httpGet);

entity= response.getEntity();

responseContent= EntityUtils.toString(entity,"UTF-8");

} catch(Exception e){

e.printStackTrace();

} finally{

try{

//关闭连接,释放资源

if(response!= null){

response.close();

}

if(httpClient!= null){

httpClient.close();

}

} catch(IOException e){

e.printStackTrace();

}

}

return responseContent;

}

/**

*发送Get请求Https

*@param httpPost

*@return

*/

private String sendHttpsGet(HttpGet httpGet){

CloseableHttpClient httpClient= null;

CloseableHttpResponse response= null;

HttpEntity entity= null;

String responseContent= null;

try{

//创建默认的httpClient实例.

PublicSuffixMatcher publicSuffixMatcher= PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString()));

DefaultHostnameVerifier hostnameVerifier= new DefaultHostnameVerifier(publicSuffixMatcher);

httpClient= HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build();

httpGet.setConfig(requestConfig);

//执行请求

response= httpClient.execute(httpGet);

entity= response.getEntity();

responseContent= EntityUtils.toString(entity,"UTF-8");

} catch(Exception e){

e.printStackTrace();

} finally{

try{

//关闭连接,释放资源

if(response!= null){

response.close();

}

if(httpClient!= null){

httpClient.close();

}

} catch(IOException e){

e.printStackTrace();

}

}

return responseContent;

}

}

如何使用HttpClient

直接上实例吧。

Apache官网下载 HttpClient,下不了的点击这里,下载完后解压取lib文件夹中jar包导入到项目中

在进行本例之前需要了解三个类

HttpClient代表Http客户端里面定义了很多http请求执行行为

HttpEntity消息载体,发送或者接收消息的载体,可以通过客户端请求或者服务器响应获取实例

HttpConnection代表http连接

本次实例代码

public class HttpCLientDemo

{

// HttpClient代表Http客户端

// HttpEntity消息载体,发送或者接收消息的载体,可以通过客户端请求或者服务器响应获取实例

// HttpConnection代表http连接

/**

*@param args

*/

public static void main(String[] args)

{

//创建默认的客户端实例

HttpClient httpCLient= new DefaultHttpClient();

//创建get请求实例

HttpGet httpget= new HttpGet("");

System.out.println("executing request"+httpget.getURI());

try

{

//客户端执行get请求返回响应实体

HttpResponse response= httpCLient.execute(httpget);

//服务器响应状态行

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

Header[] heads= response.getAllHeaders();

//打印所有响应头

for(Header h:heads){

System.out.println(h.getName()+":"+h.getValue());

}

//获取响应消息实体

HttpEntity entity= response.getEntity();

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

if(entity!= null){

//响应内容

System.out.println(EntityUtils.toString(entity));

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

//响应内容长度

System.out.println("响应内容长度:"+entity.getContentLength());

}

} catch(ClientProtocolException e){

e.printStackTrace();

} catch(IOException e){

e.printStackTrace();

}finally{

httpCLient.getConnectionManager().shutdown();

}

}

}