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. laravel强大功能路由初探(二)

    目标当然是先输出helloworld 配置hosts文件和apache下的httpd-vhosts.conf, hosts:127.0.0.1  www.blog.com httpd-vhosts.c ...

  2. html 标签

    CSS : overflow : hidden   --  就是给一个盒子定义了一个显示范围.内部的物体.只有在这个范围内部才会被显示.不然就被隐藏. overflow-x overflow-y 控制 ...

  3. Anti XSS 防跨站脚本攻击库

    https://wpl.codeplex.com/ Before understanding Anti-Cross Site Scripting Library (AntiXSS), let us u ...

  4. selenium web driver 实现截图功能

    在验证某些关键步骤时,需要截个图来记录一下当时的情况 Webdriver截图时,需要引入 import java.io.File; import java.io.IOException; import ...

  5. SQL 基础

    创建模式 create schema <schema_name> authorization <username> 没有指定schema_name时默认是用户名 删除模式 dr ...

  6. 用C3中的animation和transform写的一个模仿加载的时动画效果

    用用C3中的animation和transform写的一个模仿加载的时动画效果! 不多说直接上代码; html标签部分 <div class="wrap"> <h ...

  7. ecshop 不同页面调用不同分类文章的解决办法

    调用文章列表,需要修改对应的程序,修改index.php或者arctical_cat.php文件在$smarty->assign('new_articles', index_get_new_ar ...

  8. CString转换为string

    string CStringToString(CString strMFC) { CStringA strA; strA = strMFC.GetBuffer(); strMFC.ReleaseBuf ...

  9. 集合 LinkedList、ArrayList、Set、Treeset

    LinkedList中特有的方法: 1:方法介绍 addFirst(E e) addLast(E e) getFirst() getLast() removeFirst() removeLast() ...

  10. angularjs $broadcast $emit $on 事件触发controller间的值传递

    如何在作用域之间通信呢? 1.创建一个单例服务,然后通过这个服务处理所有子作用域的通信. 2.通过作用域中的事件处理通信.但是这种方法有一些限制:例如,你并不能广泛的将事件传播到所有监控的作用域中.你 ...