apache Httpclient GET and POST
来源:互联网 发布:橙子是啥意思网络用语 编辑:程序博客网 时间:2024/06/16 12:53
package com.yundongsports.arena.util;
/**
* Created by CF on 2017/1/6.
*/
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.springframework.beans.factory.annotation.Value;
public class HttpRequestUtils {
//成功
private static boolean TYPE=true;
@Value(“${base.web.url}”)
private static String serverHost;
/**
* 通过GET方式发起http请求
*/
public static boolean requestByGetMethod(String url){
//创建默认的httpClient实例
CloseableHttpClient httpClient = getHttpClient();
try {
//用get方法发送http请求
HttpGet get = new HttpGet(url);
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();//TODO
return TYPE=false;
}
finally{
try{
closeHttpClient(httpClient);
} catch (IOException e){
e.printStackTrace();
}
}
return TYPE;
}
/** * POST方式发起http请求 */public static boolean requestByPostMethod(String url){ CloseableHttpClient httpClient = getHttpClient(); try { HttpPost post = new HttpPost(url); //这里用上本机的某个工程做测试 //创建参数列表 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(); return TYPE=false;//TODO } catch (IOException e) { e.printStackTrace(); } finally{ try{ closeHttpClient(httpClient); } catch(Exception e){ e.printStackTrace(); } } return TYPE;}private static CloseableHttpClient getHttpClient(){ return HttpClients.createDefault();}private static void closeHttpClient(CloseableHttpClient client) throws IOException{ if (client != null){ client.close(); }}public static void main(String[] args) { boolean a= requestByGetMethod("http://192.168.2.163:8999/"); System.out.print("a = " + a);}
}
maven————————————————————————–:
org.apache.httpcomponents
httpclient
4.3.5
- apache Httpclient GET and POST
- 使用Apache HttpClient发送get、post请求
- apache httpclient新版本的get/post请求
- get、post、httpclient-get、httpclient-post
- 運用Apache HttpClient實作Get與Post動作
- android使用Apache HttpClient实现get/post的请求
- Android用Apache HttpClient 实现POST和Get请求
- Android使用Apache HttpClient发送GET、POST请求
- Android用Apache HttpClient 实现POST和Get请求
- org.apache.http.client.HttpClient get/post请求
- android网络编程Apache——HttpClient(GET。POST)
- httpClient get post 实例
- HttpClient get, post使用
- Android HttpClient POST GET
- HttpClient Get和Post
- httpClient post/get请求。
- HttpClient get,Post请求
- httpClient-Post-Get代码
- SpringMVC注解@initbinder解决类型转换问题
- spring4学习日记——第一天
- HttpMessageConverter
- 华陆工程——业务流程管理方案
- java集合
- apache Httpclient GET and POST
- angular2系统学习 - 管道
- 计算机底层是如何访问显卡的?
- 吕学敏:全面、细致功能锻造灵活审批
- IScroll 图片导致refresh不准确问题
- 20-电子词典
- jQuery 原型技术分解一
- 独家!深入探访快播团队年会现场,见证快播团队再次崛起!
- 复杂数据类型引用做函数参数