1、get
public static String doGet(String strUrl ){
String strReturn="";
HttpGet httpGet = new HttpGet(strUrl);
CloseableHttpClient httpclient = null;
CloseableHttpResponse response1=null;
try {
httpclient = HttpClients.createDefault();
response1 = httpclient.execute(httpGet);
HttpEntity entity1 = response1.getEntity();
strReturn=EntityUtils.toString(entity1) ;
EntityUtils.consume(entity1);
}catch(Exception e){
e.printStackTrace();
}finally {
try {
if(response1!=null)
response1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return strReturn;
}
2、put
public static String doPut(String strUrl,String param){
CloseableHttpClient httpclient = HttpClients.createDefault();
String strReturn="";
PutMethod httpput=new PutMethod(strUrl);
try {
if(param!=null)
{
RequestEntity entity = new StringRequestEntity(param, "application/json", "UTF-8");
httpput.setRequestEntity(entity);
}
httpClient.executeMethod(httpput);
byte[] bytes = httpput.getResponseBody();
strReturn= new String(bytes) ;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strReturn;
}
3、post
public static String doPost(String requestUrl, String payload) {
CloseableHttpClient httpclient = HttpClients.createDefault();
String strReturn="";
PostMethod httpost = new PostMethod(requestUrl);
try {
if(payload!=null)
{
RequestEntity entity = new StringRequestEntity(payload, "application/json", "UTF-8");
httpost.setRequestEntity(entity);
}
httpClient.executeMethod(httpost);
byte[] bytes = httpost.getResponseBody();
strReturn= new String(bytes) ;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return strReturn;
}
4、delete
public static void doDelete(String urlToRead) throws Exception {
URL url = new URL(urlToRead);
HttpURLConnection httpCon = (HttpURLConnection) url.openConnection();
httpCon.setDoOutput(true);
httpCon.setRequestProperty(
"Content-Type", "application/x-www-form-urlencoded" );
httpCon.setRequestMethod("DELETE");
httpCon.connect();
httpCon.disconnect();
}
加个广告: http://51msg.net http://008sou.com http://s.008sou.com http://ask.008sou.com
分享到:
相关推荐
是一个Java 发送http put、delete、 post、 get 请求的工具类。可用在与restful service 进行通讯的代码中。
服务端模拟ajax,支持发起GET、POST、PUT、DELETE请求
测试接口,模拟POST,GET,PUT,DELETE提交的工具
HttpClient工具类,简单轻松的实现get,post,put和delete请求
Okhttps添加头部Token值 和关于 GET POST DELETE PUT 请求同步异步网络的一个封装。
Rest client是用来调试REST风格的Web服务,接收POST, GET, PUT, DELETE请求。在chrome或者firefox浏览器都有很多插件。目前发现chrome浏览器的advanced rest client的功能比较强大。因此介绍一下使用插件的安装与...
UniApp中封装request网络请求包含GET、POST、PUT、DELETE等,更加方便实现网络数据请求,包含项目的基本框架目录,可直接用于项目开发,适用于app和微信小程序等项目
java请求接口方式java-get-post-delete-put-请求client
Qt http同步请求测试(restfull中常用的get/post/put/delete、文件上传/下载) 示例源码 https://blog.csdn.net/aggs1990/article/details/124104548 CSDN审核可能较慢,如无法下载,可以过段时间再回来看下
主要实现HTTP POST/GET/PUT/DELETE等基本操作方式,此外还实现了代理、Header读取、HTML解析特定标签的属性值。
http接口通用调试工具,支持get、post、put、patch、delete方法,数据的发送和接收使用json格式,即Content-Type: application/json
详解http的put delete get post
这是一个关于get,put相关介绍,希望大家能够有所收获,谢谢大家
在config.json里设置数据库连接字符串及服务端口等参数,REST服务提供POST、GET、PUT、DELETE4种请求,SQL服务可执行INSERT、SELECT、UPDATE、DELETE4种SQL语句;请求结果返回JSON字符串(以后加上JSON SCHEMA)。...
用来测试webapi 后台 接收前台数据的代码,写的有点乱,需要自行查看。本来想把代码写进md文档的,后来嫌麻烦
HTTP定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE。URL全称是资源描述符。我们可以这样认为:?一个URL地址,它用于描述一个网络上的资源,而HTTP中的GET,POST,PUT,DELETE就...
包含post、delete、put、get方法四个restful支持的服务,客户端向服务端发出的post等四个操作的http头,并携带参数向服务端获取响应。 实现了book的增删改查
Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE。URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而HTTP中的GET,POST,PUT,DELETE就...
spring rest 增删改查(put\delete\post\get)简单示例详细讲解,亲测可用。spring rest 增删改查(put\delete\post\get)简单示例详细讲解,亲测可用。spring rest 增删改查(put\delete\post\get)简单示例详细...