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. 关于ubuntu下sublime text 3 的安装和中文配置问题

    一.sublime text 3 在ubuntu 16.04下的安装过程 1)首先下载sublime text 3 的tar包 $ wget https://download.sublimetext. ...

  2. 多个文件下载打包生成zip格式下载

    这个多个文件下载生成zip格式必须先引用一个ICSharpCode.SharpZipLib.dll. 代码如下  //将多个文件打包成压缩文件zip格式下载         protected voi ...

  3. .NET中的异步

    .NET中4种异步方式? ThreadPool.QueueUserworkItem实现 APM模式(就是BeginXXX和EndXXX成对出现.) EAP模式(就是Event based, 准确说来就 ...

  4. 推荐eclipse插件Properties Editor

    需求:一般我们在做"国际化"功能时,我们需要properties中文表示方式用unicode表示.eclipse默认properties文件编辑器不方便查看,需要我们查看常常查找u ...

  5. C#小方法PadLeft 和 PadRight

    1.在 C# 中可以对字符串使用 PadLeft 和 PadRight 进行轻松地补位. PadLeft(int totalWidth, char paddingChar) //在字符串左边用 pad ...

  6. 关于linux下system()函数的总结

    导读 曾经的曾经,被system()函数折磨过,之所以这样,是因为对system()函数了解不够深入.这里必须要搞懂system()函数,因为有时你不得不面对它. 先来看一下system()函数的简单 ...

  7. Memcache的增删改查

    Memcache是把数据存放到内存的一种缓存技术,为了提高访问的速度,memcache存储的数据一般是频繁.不太重要的数据,php使用memcache,需要两步: (1).php_memcache.d ...

  8. Can I Win

    In the "100 game," two players take turns adding, to a running total, any integer from 1.. ...

  9. iOS Salesforce SDK 小知识

    Salesforce SDK 能做许多任务,因此也比较繁杂,又分了原生,js等多个调用方法. 关键点总结: SFSmartStore 中的 - (id) initWithName:(NSString* ...

  10. Qt 二维码

    1.生成二维码 利用第三方库qrencode ,将qrencode源码添加到自己的程序中,直接调用使用. 参考http://blog.csdn.net/zhangxufei/article/detai ...