码迷,mamicode.com
首页 > 编程语言 > 详细

[Java]随记--HttpClient发送put请求

时间:2017-09-06 10:01:13      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:addheader   head   stack   ram   exce   red   href   gets   ref   

http://blog.csdn.net/u010989191/article/details/52852155

**********************************************************

   @RequestMapping(value="/Xxxxx/authenticate")
    public @ResponseBody String getToken(@RequestParam String username, @RequestParam String password) throws Exception {
        InputStream is = null;
        BufferedReader br = null;
        StringBuilder sBuilder = null;
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpPut httpPut = new HttpPut("url");
            httpPut.addHeader("username",username);
            httpPut.addHeader("password",password);
            HttpResponse httpResponse = httpClient.execute(httpPut);
            //连接成功
            if(200 == httpResponse.getStatusLine().getStatusCode()){
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();
                br = new BufferedReader(new InputStreamReader(is));
                String tempStr;
                sBuilder = new StringBuilder();
                while ((tempStr = br.readLine()) != null) {
                    sBuilder.append(tempStr);
                }
                br.close();
                is.close();
            }
        }catch (Exception e){
            System.out.println(e);
            e.printStackTrace();
        }
        return sBuilder==null? "":sBuilder.toString();
    }

所需的jar包:http://download.csdn.net/detail/u010989191/9657039

 

[Java]随记--HttpClient发送put请求

标签:addheader   head   stack   ram   exce   red   href   gets   ref   

原文地址:http://www.cnblogs.com/zhao1949/p/7482814.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!