HTTPUTILS
maven依赖
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
工具类
package com.yyjdemo.shardingjdbc.http; import com.alibaba.fastjson.JSONObject;
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.client.utils.URIBuilder;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils; import java.io.IOException;
import java.util.ArrayList;
import java.util.List; public class HttpUtils {
public static void main(String[] args) throws IOException { JSONObject jsonObject = new JSONObject();
jsonObject.put("id","1");
// String httppost = httppost("http://127.0.0.1:8080/jdbc/testpost",jsonObject);
// System.out.println(httppost);
// String httppost = httppostjson("http://127.0.0.1:8080/jdbc/testpostjson",jsonObject);
String httppost = httpget("http://127.0.0.1:8080/jdbc/test",jsonObject);
} public static String httpget(String url, JSONObject jsonObject) {
try {
URIBuilder uriBuilder = new URIBuilder(url);
if(!jsonObject.isEmpty()){
jsonObject.forEach((k,v)->{
uriBuilder.addParameter(k,v.toString());
});
} HttpGet httpget = new HttpGet(uriBuilder.toString());
try(CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse execute = httpclient.execute(httpget);){
HttpEntity entity = execute.getEntity();
String s = EntityUtils.toString(entity);
return s;
}
}catch (Exception e){
e.printStackTrace();
}
return null;
} public static String httppostjson(String url, JSONObject jsonObject) {
try {
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE,"application/json");
StringEntity stringEntity = new StringEntity(jsonObject.toString(),"utf-8");
httpPost.setEntity(stringEntity);
try(CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse execute = httpclient.execute(httpPost);){
HttpEntity entity = execute.getEntity();
String s = EntityUtils.toString(entity);
return s;
}
}catch (Exception e){
e.printStackTrace();
}
return null;
} private static String httppost(String url,JSONObject jsonObject){
try {
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> objects = new ArrayList<>();
jsonObject.forEach((k,v)->{
BasicNameValuePair basicNameValuePair = new BasicNameValuePair(k, v.toString());
objects.add(basicNameValuePair);
});
StringEntity stringEntity = new UrlEncodedFormEntity(objects,"utf-8");
httpPost.setEntity(stringEntity);
try(CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse execute = httpclient.execute(httpPost);){
HttpEntity entity = execute.getEntity();
String s = EntityUtils.toString(entity);
return s;
}
}catch (Exception e){
e.printStackTrace();
}
return null;
}
}
HTTPUTILS的更多相关文章
- XUtils框架中HttpUtils使用Get请求时总是返回相同信息的问题解决,xutilshttputils
如需转载请标明出处:http://blog.csdn.net/itas109 版本:Xutils 2014年11月11日 下载地址:https://github.com/wyouflf/xUtils ...
- 使用httputils上传图片到服务器
//创建httpUtils对象 HttpUtils mRegHttpUtils = new HttpUtils(); //图片路径 String path = "/sdcard/Downlo ...
- 3. Android框架和工具之 xUtils(HttpUtils)
1. HttpUtils 作用: 支持同步,异步方式的请求: 支持大文件上传,上传大文件不会oom: 支持GET,POST,PUT,MOVE,COPY,DELETE,HEAD请求: 下载支持301/3 ...
- 反射机制及开源框架xUitls的使用,使用HttpUtils通过断点续传下载文件
反射: Java反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法: 对于任意一个对象,都能够调用它的任意一个方法和属性 Java反射机制主要提供下面几种用途: 1•在运行时判断 ...
- xutils的HttpUtils,Post和Get基本使用,以及BitmapUtils的简单使用
开篇报错注意:本教程是基于xUtils-2.6.14.jar版本实现的 由于studio中6.0以后安卓取消了httpclient,而xutils则基于httpclient开发的,所以现在无法使用,将 ...
- XUtils骨架HttpUtils采用Get总是返回请求解决问题的相同信息
如需转载请注明出处:http://blog.csdn.net/itas109 版本号:Xutils 2014年11月11日 下载地址:https://github.com/wyouflf/xUtils ...
- Androids含文档erver结束(工具包 Httputils)两
在同server在...的基础上,本文client还登录界面 Andriod简单http get请求基础上,用户注冊后跳转到下载界面,本文下载界面仅仅有两个View,一个是textView显示注冊后u ...
- 客户端 HttpUtils.java
package com.http.post; import java.io.ByteArrayOutputStream; import java.io.IOException; import java ...
- 使用HttpUtils 上传视频文件
private void shangchuan(){ //文件的路径 //File file=new File(path); File fi ...
- 使用HttpUtils完成Http Basic 认证
调用声网(agora)的远程接口(Restful Api)时,对方需要使用Basic Auth的方式进行认证(需要输入用户名和密码). 一,使用Postman完成基于Basic Auth的Http认证 ...
随机推荐
- Python+Selenium笔记(十五)调用JS
(一) 方法 方法 简单说明 execute_async_script(script, args) 异步执行JS代码 script:被执行的JS代码 args:js代码中的任意参数 execute_s ...
- Oracle EBS 新增 OAFM 个数
在 $INST_TOP/ora/10.1.3/opmn/conf/opmn.xml中 找到 <process-type id="oafm" module-id="O ...
- PXE 和 计算机网络启动
1. 什么是PXE PXE(Pre-boot Execution Environment,预启动执行环境)是由Intel公司开发的最新技术,工作于Client/Server的网络模式,支持工作站通过网 ...
- Python学习--Selenium模块学习(2)
Selenium的基本操作 获取浏览器驱动寻找方式 1. 通过手动指定浏览器驱动路径2. 通过 `$PATH`环境变量找寻浏览器驱动 可参考Python学习--Selenium模块简单介绍(1) 控制 ...
- 过滤掉URL中的参数部分
//将超链接的参数部分滤掉 ?xxx if(url.contains("?")){ url=url.substring(0,url.indexOf("?")); ...
- 56_实现类似spring的可配置的AOP框架
> config.properties 配置文件 key=类名 > BeanFactory Bean工厂,负责得到bean getBean("xxx") &g ...
- MySQL基础之 支持的数据类型
MySQL的数值类型 整数类型 字节 有符号 无符号 TINYINT 1 -128~+127 0~255 SAMLLINT 2 -32768~+32767 0~65535 MEDIUMINT 3 -8 ...
- Hadoop HBase概念学习系列之HBase里的Zookeeper(二十一)
这个,很简单,但凡是略懂大数据的,就很清楚,不多说,直接上图.
- Python sys.md
sys-System-specific Configuration Interpreter Settings sys包含用于访问解释器的编译时或运行时配置设置的属性和函数. Build-time Ve ...
- python第四十三课——封装性
1.面向对象的三大特性:封装性.继承性.多态性 封装: 封装使用的领悟: 1).生活层面:食品.快递.计算机.明星... 2).计算机层面: ①.模块.类.函数... ②.属性数据的封装与隐藏 权限修 ...