博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JAVA 实现 GET、POST、PUT、DELETE HTTP请求
阅读量:5241 次
发布时间:2019-06-14

本文共 3381 字,大约阅读时间需要 11 分钟。

1、get

Java代码  
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

Java代码  
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

Java代码  
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

Java代码  
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();                                 }

 

MAVEN

commons-httpclient
commons-httpclient
3.1

 

转载于:https://peter1981.iteye.com/blog/2347869

转载于:https://www.cnblogs.com/java-h/p/11101723.html

你可能感兴趣的文章
软件测试——性能测试总结
查看>>
12.4站立会议
查看>>
Java Concurrentmodificationexception异常原因和解决方法
查看>>
客户端访问浏览器的流程
查看>>
codeforces水题100道 第二十二题 Codeforces Beta Round #89 (Div. 2) A. String Task (strings)
查看>>
c++||template
查看>>
[BZOJ 5323][Jxoi2018]游戏
查看>>
编程面试的10大算法概念汇总
查看>>
Vue
查看>>
python-三级菜单和购物车程序
查看>>
条件断点 符号断点
查看>>
VMware12 + Ubuntu16.04 虚拟磁盘扩容
查看>>
水平垂直居中
查看>>
MySQL简介
查看>>
设计模式之桥接模式(Bridge)
查看>>
jquery的$(document).ready()和onload的加载顺序
查看>>
Python Web框架Django (五)
查看>>
.net学习之继承、里氏替换原则LSP、虚方法、多态、抽象类、Equals方法、接口、装箱拆箱、字符串------(转)...
查看>>
【codevs1033】 蚯蚓的游戏问题
查看>>
【程序执行原理】
查看>>