java httpclient工具类_httpClientUtils工具类发送GET或POST请求完整案例

httpClient是javaEE语言中用于接口调用的框架,本案例封装好了httpClientUtils工具类,用于发送GET和POST请求,直接将此工具类复制到你的项目中就可以了。

第一步:httpClient jar包可采用maven方式下载,version版本自己改,如下;

javax.servlet.jsp

jsp-api

2.2

commons-httpclient

commons-httpclient

3.1

第二步:新建httpClientUtils.java工具类,将此内容复制到你的项目中。package cn.com.baidu.util;

import java.io.IOException;

import java.io.InputStream;

import java.util.ArrayList;

import java.util.List;

import java.util.Map;

import org.apache.http.HttpEntity;

import org.apache.http.HttpHeaders;

import org.apache.http.HttpResponse;

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;

public class HttpClientUtils {

public static String getContentFromUrl(String url) {

CloseableHttpClient httpClient = HttpClients.createDefault();

try {

HttpGet request = new HttpGet(url);

request.setHeader(HttpHeaders.USER_AGENT,

"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");

request.setHeader(HttpHeaders.ACCEPT, "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");

request.setHeader(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate, sdch");

request.setHeader(HttpHeaders.CONNECTION, "keep-alive");

CloseableHttpResponse httpResponse = null;

httpResponse = httpClient.execute(request);

try {

HttpEntity entity = httpResponse.getEntity();

if (null != entity) {

return EntityUtils.toString(entity);

}

} finally {

httpResponse.close();

}

} catch (Exception e) {

e.printStackTrace();

} finally {

try {

if (httpClient != null) {

httpClient.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return null;

}

public static InputStream getStreamFromUrl(String url) {

CloseableHttpClient httpClient = HttpClients.createDefault();

HttpGet request = new HttpGet(url);

request.setHeader(HttpHeaders.USER_AGENT,

"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.103 Safari/537.36");

request.setHeader(HttpHeaders.ACCEPT,

"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");

request.setHeader(HttpHeaders.ACCEPT_ENCODING, "gzip, deflate, sdch");

request.setHeader(HttpHeaders.CONNECTION, "keep-alive");

CloseableHttpResponse httpResponse = null;

try {

httpResponse = httpClient.execute(request);

HttpEntity entity = httpResponse.getEntity();

if (null != entity) {

return entity.getContent();

}

} catch (Exception e) {

e.printStackTrace();

}

return null;

}

public static String doPost(String url,Map map,String charset){

CloseableHttpClient httpClient = HttpClients.createDefault();

HttpPost httpPost = new HttpPost(url);;

String result = null;

try{

//设置参数

List list = new ArrayList();

//遍历map集合

for(Map.Entry entry :map.entrySet()){

list.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));

}

if(list.size() > 0){

//charset=utf-8或者gbk2312

UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);

httpPost.setEntity(entity);

}

HttpResponse response = httpClient.execute(httpPost);

if(response != null){

HttpEntity resEntity = response.getEntity();

if(resEntity != null){

result = EntityUtils.toString(resEntity,charset);

}

}

}catch(Exception ex){

ex.printStackTrace();

}finally {

if (httpClient != null) {

try {

httpClient.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

return result;

}

}

第三步:使用httpClientUtils工具类发送get,post请求案例。public class TestMain {

//发送get请求,拼接参数

public void testPost(){

String url = "https://192.168.1.101/login?username=xxx&password=xxxx";

String result = httpClientUtils.getContentFromUrl(url);

System.out.println("get登录结果:"+result);

}

//发送post请求,form表单参数

public void testPost(){

String url = "http://192.168.1.101/login";

String charset = "utf-8";

Map loginMap = new HashMap();

loginMap.put("username","*****");

loginMap.put("password","*****");

String result = httpClientUtils.doPost(url,loginMap,charset);

System.out.println("post登录结果:"+result);

}

public static void main(String[] args){

testPost();

testPost();

}

}

来源网站:太平洋学习网,转载请注明出处:http://www.tpyyes.com/a/javaweb/115.html

相关资源:HttpClientUtils
已标记关键词 清除标记
表情包
插入表情
评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符
相关推荐
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页