package JanGin.httpClient.demo;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List; 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;
import org.apache.http.util.EntityUtils;
import org.junit.Test; public class FirstDemo { /**
* 通过GET方式发起http请求
*/
@Test
public void requestByGetMethod(){
//创建默认的httpClient实例
CloseableHttpClient httpClient = getHttpClient();
try {
//用get方法发送http请求
HttpGet get = new HttpGet("http://www.baidu.com");
System.out.println("执行get请求:...."+get.getURI());
CloseableHttpResponse httpResponse = null;
//发送get请求
httpResponse = httpClient.execute(get);
try{
//response实体
HttpEntity entity = httpResponse.getEntity();
if (null != entity){
System.out.println("响应状态码:"+ httpResponse.getStatusLine());
System.out.println("-------------------------------------------------");
System.out.println("响应内容:" + EntityUtils.toString(entity));
System.out.println("-------------------------------------------------");
}
}
finally{
httpResponse.close();
}
} catch (Exception e) {
e.printStackTrace();
}
finally{
try{
closeHttpClient(httpClient);
} catch (IOException e){
e.printStackTrace();
}
} } /**
* POST方式发起http请求
*/
@Test
public void requestByPostMethod(){
CloseableHttpClient httpClient = getHttpClient();
try {
HttpPost post = new HttpPost("http://localhost/...."); //这里用上本机的某个工程做测试
//创建参数列表
List<NameValuePair> list = new ArrayList<NameValuePair>();
list.add(new BasicNameValuePair("j_username", "admin"));
list.add(new BasicNameValuePair("j_password", "admin"));
//url格式编码
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(list,"UTF-8");
post.setEntity(uefEntity);
System.out.println("POST 请求...." + post.getURI());
//执行请求
CloseableHttpResponse httpResponse = httpClient.execute(post);
try{
HttpEntity entity = httpResponse.getEntity();
if (null != entity){
System.out.println("-------------------------------------------------------");
System.out.println(EntityUtils.toString(uefEntity));
System.out.println("-------------------------------------------------------");
}
} finally{
httpResponse.close();
} } catch( UnsupportedEncodingException e){
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
finally{
try{
closeHttpClient(httpClient);
} catch(Exception e){
e.printStackTrace();
}
} } private CloseableHttpClient getHttpClient(){
return HttpClients.createDefault();
} private void closeHttpClient(CloseableHttpClient client) throws IOException{
if (client != null){
client.close();
}
}
}

HttpClient发送Get和Post请求的更多相关文章

  1. HttpClient发送get,post接口请求

    HttpClient发送get post接口请求/*  * post  * @param url POST地址 * @param data POST数据NameValuePair[] * @retur ...

  2. Android笔记---使用HttpClient发送POST和GET请求

    在Android上发送 HTTP 请求的方式一般有两种, HttpURLConnection 和 HttpClient,关于HttpURLConnection的使用方法能够參考HTTP之利用HttpU ...

  3. Java实现HttpClient发送GET、POST请求(https、http)

    1.引入相关依赖包 jar包下载:httpcore4.5.5.jar    fastjson-1.2.47.jar maven: <dependency> <groupId>o ...

  4. 使用httpclient发送get或post请求

    HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的.最新的.功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建 ...

  5. HttpClient 发送 HTTP、HTTPS 请求的简单封装

    import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.Http ...

  6. [java,2018-01-16] HttpClient发送、接收 json 请求

    最近需要用到许多在后台发送http请求的功能,可能需要发送json和xml类型的数据. 就抽取出来写了一个帮助类: 首先判断发送的数据类型是json还是xml: import org.dom4j.Do ...

  7. 读取配置文件的URL,使用httpClient发送Post和Get请求,实现查询快递物流和智能机器人对话

    1.主要jar包: httpclient-4.3.5.jar   httpcore-4.3.2.jar 2.目录结构如图所示: 3.url.properties文件如下: geturl=http:// ...

  8. 【httpclient-4.3.1.jar】httpclient发送get、post请求以及携带数据上传文件

    1.发送get.post携带参数以及post请求接受JSON数据: package cn.qlq.utils; import java.io.BufferedReader; import java.i ...

  9. java使用HttpClient 发送get、pot请求

    package eidolon.messageback.PostUtil; import java.io.BufferedReader; import java.io.IOException; imp ...

随机推荐

  1. 安装centos6.5如何选择安装包

    (1)系统安装类型选择及自定义额外包组 进入如图2-28所示界面.上半部分是系统定制的不同的系统安装类型选择项,默认是"Desktop",这里我们选择"Minimal&q ...

  2. runtime理论知识

    http://southpeak.github.io/2014/10/25/objective-c-runtime-1/ 转载http://www.jianshu.com/p/6b905584f536 ...

  3. UIAlertController

    楼主在整理项目的警告,于是乎你懂的. 然后自己整理了一下以后方便自己忘了之后能及时找到它 关于UIAlertController .h文件的解析 /** 关于UIAlertController的解析 ...

  4. CentOS光盘挂载命令以及安装软件

    最近又学习了一个命令:mount 挂载命令,我们在安装软件的时候,直接敲命令install 包名,但是这里其实是联网安装的, 如果使用光盘,从本地安装就要使用mount命令. 1.我的linux系统是 ...

  5. $_SERVER 详情

    $_SERVER['HTTP_ACCEPT_LANGUAGE']//浏览器语言 $_SERVER['REMOTE_ADDR'] //当前用户 IP . $_SERVER['REMOTE_HOST'] ...

  6. 什么时候用Model,什么时候用Entity?

    在建立一个实体类的时候,究竟是用Model还是用Entity?比如MVC中,Model存了数据实体,但是他被称为Model,而在EF中,Entity也是存放数据实体,却被称作Entity,这两者有何区 ...

  7. MySql的一些用法

    1.怎样找到MySql数据的存储目录? 答:从服务中查看正在运行的MySql,查看它的启动参数,可能是这个样子: "D:\Program Files\MySQL\MySQL Server 5 ...

  8. Reverse Core 第三部分 - 21章 - Windows消息钩取

    @author: dlive @date: 2016/12/19 0x01 SetWindowsHookEx() HHOOK SetWindowsHookEx( int idHook, //hook ...

  9. mysql安装时到最后start service时就不响应了的解决方法

  10. CodeForces 589J Cleaner Robot

    题目链接 题意:一个机器人打扫卫生,URDL代表初始时机器人面对的方向上右下左. ' . ' 代表可以打扫的, ' * ' 代表家具,如果机器人遇到家具就顺时针转90度,问机器人能打扫多少面积. 题解 ...