分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

httpclient的简单使用

发布时间:2023-09-06 01:07责任编辑:赖小花关键词:http

1、通过get请求后台,注意tomcat的编码设置成utf-8;    <Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1" redirectPort="8443" URIEncoding="UTF-8" />

/** ?????* 发送 get请求 ?????*/ ?????public static void get() { ?????????CloseableHttpClient httpclient = HttpClients.createDefault(); ?????????try { ?????????????//先将参数放入List,再对参数进行URL编码 ????????????List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>(); ????????????params.add(new BasicNameValuePair("get", "get请求哈哈哈")); ????????????//对参数编码 ????????????String param = URLEncodedUtils.format(params, "UTF-8"); ????????????// 创建httpget. ???????????????HttpGet httpget = new HttpGet("http://localhost:8080/HttpServleTest.html?"+param); ?????????????// 执行get请求. ???????????????CloseableHttpResponse response = httpclient.execute(httpget); ?????????????try { ?????????????????// 获取响应实体 ???????????????????HttpEntity entity = response.getEntity(); ????????????????// 打印响应状态码 ???????????????????System.out.println(response.getStatusLine().getStatusCode()); ?????????????????if (entity != null) { ?????????????????????// 打印响应内容 ???????????????????????System.out.println("Response content: " + EntityUtils.toString(entity,"UTF-8")); ?????????????????} ?????????????} finally { ?????????????????response.close(); ?????????????} ?????????} catch (Exception e) { ?????????????e.printStackTrace(); ?????????} finally { ?????????????// 关闭连接,释放资源 ???????????????try { ?????????????????httpclient.close(); ?????????????} catch (IOException e) { ?????????????????e.printStackTrace(); ?????????????} ?????????} ?????} ?

2.post请求

/** ?????* 发送 post ????*/ ?????public static void post() { ?????????// 创建默认的httpClient实例. ???????????CloseableHttpClient httpclient = HttpClients.createDefault(); ?????????// 创建httppost ???????????HttpPost httppost = new HttpPost("http://localhost:8080/HttpServleTest.html"); ?????????// 创建参数队列 ???????????List<NameValuePair> formparams = new ArrayList<NameValuePair>(); ?????????formparams.add(new BasicNameValuePair("post", "post请求哈哈哈")); ?????????UrlEncodedFormEntity uefEntity; ?????????try { ?????????????uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); ?????????????httppost.setEntity(uefEntity); ?????????????CloseableHttpResponse response = httpclient.execute(httppost); ?????????????try { ?????????????????HttpEntity entity = response.getEntity(); ?????????????????if (entity != null) { ?????????????????????System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8")); ?????????????????} ?????????????} finally { ?????????????????response.close(); ?????????????} ?????????} catch (Exception e) { ?????????????e.printStackTrace(); ?????????} finally { ?????????????// 关闭连接,释放资源 ???????????????try { ?????????????????httpclient.close(); ?????????????} catch (IOException e) { ?????????????????e.printStackTrace(); ?????????????} ?????????} ?????} ?

3、后台服务程序和本案例代码下载地址:

httpclient的简单使用

原文地址:http://www.cnblogs.com/zhangjinru123/p/7499138.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved