SpringBoot 接口开发(httpclient客户端)

服务端


@RestController
@RequestMapping("/landary")
public class landaryController {

    @RequestMapping("adduser")
    public JSONObject addUser(@RequestBody JSONObject userEntity)
    {
        System.out.println(JSONObject.toJSONString(userEntity));
        JSONObject json=new JSONObject();
        json.fluentPut("code","500").fluentPut("result",userEntity);
        return json;
    }

    @RequestMapping("showuser")
    public Object showUser()
    {
        return JSON.toJSONString("hhh");
    }

}


客户端post请求

 public static String sendSms(String uid,String title,String content){
        HttpClient httpclient = new DefaultHttpClient();


        String smsUrl="http://127.0.0.1:8088/landary/adduser";
        HttpPost httppost = new HttpPost(smsUrl);
        String strResult = "";

        try {
            JSONObject jobj = new JSONObject();
            jobj.put("uid", uid);
            jobj.put("title", title);
            jobj.put("content",content);

            System.out.println(jobj.toString());
         //   nameValuePairs.add(new BasicNameValuePair("msg", (jobj.toString())));
    /*        httppost.addHeader("Content-type", "application/json; charset=utf-8");
            httppost.setHeader("Accept", "application/json");
            httppost.setEntity(new StringEntity(jobj.toString(), Charset.forName("UTF-8")));*/

           StringEntity s = new StringEntity(jobj.toString());
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");//发送json数据需要设置contentType
            httppost.setEntity(s);
            HttpResponse response = httpclient.execute(httppost);
            if (response.getStatusLine().getStatusCode() == 200) {
					/*读返回数据*/
                String conResult = EntityUtils.toString(response
                        .getEntity());
                System.out.println(conResult);
               JSONObject sobj = new JSONObject();
               sobj = JSONObject.parseObject(conResult);
                String result = sobj.getString("result");
                String code = sobj.getString("code");
                if(code.equals("500")){
                    System.out.println(result);
                    strResult += "发送成功";
                }else{
                    strResult += "发送失败,"+code;
                }

            } else {
                String err = response.getStatusLine().getStatusCode()+"";
                strResult += "发送失败:"+err;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return strResult;
    }


get请求

/**
     * 发送 get请求
     */
    public void get() {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            // 创建httpget.
            HttpGet httpget = new HttpGet("http://127.0.0.1:8088/landary/showuser");
            System.out.println("executing request " + httpget.getURI());
            // 执行get请求.
            CloseableHttpResponse response = httpclient.execute(httpget);
            try {
                // 获取响应实体
                HttpEntity entity = response.getEntity();
                System.out.println("--------------------------------------");
                // 打印响应状态
                System.out.println(response.getStatusLine());
                if (entity != null) {
                    // 打印响应内容长度
                    System.out.println("Response content length: " + entity.getContentLength());
                    // 打印响应内容
                    System.out.println("Response content: " + EntityUtils.toString(entity));
                }
                System.out.println("------------------------------------");
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


  • 1
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值