• 技术文章 >Java >java教程

    Java的实现得到,PUT,POST,删除请求

    巴扎黑巴扎黑2017-03-28 15:36:17原创1344
    Java的实现得到,PUT,POST,删除请求:

    1,获得

    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,放

    public static String doPut(String strUrl,String param){ 
         CloseableHttpClient httpclient = HttpClients.createDefault();
         StringBuffer jsonString= new StringBuffer();
             try {
             final HttpPut put=new HttpPut(strUrl);
             put.setEntity(new StringEntity(param,"UTF-8")); 
                CloseableHttpResponse response1= httpclient.execute(put ); 
                 try {
                     HttpEntity entity1 = response1.getEntity();
                     BufferedReader br = new BufferedReader(new InputStreamReader(entity1.getContent())); 
                     String line;
                     while ((line = br.readLine()) != null) {
                             jsonString.append(line);
                     }  
                     
                     EntityUtils.consume(entity1);
                 } finally {
                     response1.close();
                 }
             }catch(Exception e){
             e.printStackTrace();
             }
             return jsonString.toString();
        }

    3,岗位

    public static String doPost(String requestUrl, String payload) {
        String strReturn="";  
       PostMethod httpost = new PostMethod(requestUrl);   
       httpost.setRequestBody(payload);  
    try { 
           httpClient.executeMethod(httpost);
           byte[] bytes = httpost.getResponseBody(); 
           strReturn=  new String(bytes) ; 
    } catch (Exception e) { 
    e.printStackTrace();
    }
        return strReturn;
       }

    4,删除

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

    以上就是Java的实现得到,PUT,POST,删除请求的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!

    相关文章:

    详细介绍Java编程常见问题汇总

    Java多线程基础详解

    Java远程通讯技术及原理分析的图文介绍

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:Java,PUT,POST
    上一篇:List排序 下一篇:为什么不提供JUnit的方法assertNotEquals?
    千万级数据并发解决方案

    相关文章推荐

    • 一起来分析java是值传递还是引用传递• Java实例详解之子线程任务异常,主线程事务回滚• 详细整理java枚举的使用总结• 一起聊聊Java常用数据类型的输入输出• 带你搞懂JAVA反射机制(总结分享)
    1/1

    PHP中文网