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

Posted zhao1949

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Java]随记--HttpClient发送put请求相关的知识,希望对你有一定的参考价值。

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请求的主要内容,如果未能解决你的问题,请参考以下文章

接口测试:postman和jmeter随记

HttpClient基本使用

JAVA通过HttpClient发送HTTP请求的方法

在.NET Core使用 HttpClient 的正确方式

SQL随记

java httpclient发送json 请求 ,go服务端接收