SpringBoot 接口开发(httpclient客户端)
来源:互联网 发布:matlab2017a for mac 编辑:程序博客网 时间:2024/05/21 14:54
服务端
@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(); } } }
阅读全文
0 0
- SpringBoot 接口开发(httpclient客户端)
- Java 客户端HttpClient 开发
- restful接口客户端和服务端开发,HttpURLConnection,HttpClient,post ,get方式调用
- HTTP接口客户端开发
- Android开发之Http通信HttpClient接口
- java web HttpClient 开发一个接口
- HttpClient模拟客户端向 服务器接口 传递 Json 数据
- HttpClient客户端
- PHP开发移动客户端接口---封装接口
- springboot 集成httpclient
- SpringBoot开发详解(一)--初识SpringBoot
- httpclient客户端thread+handler+message(get)
- 业务接口造数据(httpclient)
- Springboot学习笔记(四)Repository接口
- 为 Android 开发访问 JAX-RS Web 服务的 Apache HttpClient 客户端 学习创建 Apache HttpClient Android 客户端
- CXF客户端调用?wsdl webservice接口开发
- 手机客户端接口开发文档模板整理
- 支付宝接口开发:客户端服务器端
- JavaWeb学习篇之----HTTP协议详解
- iOS积累--textField获取键盘高度以及限制字数
- Linux 网络管理
- 【Centos7笔记十】NFS的安装使用
- Java的Try-Catch中return分析
- SpringBoot 接口开发(httpclient客户端)
- 深入理解Javascript中的原型、原型链、继承
- 改变手机状态栏颜色常见的2种方法
- Tomcat 隐藏 CMD 窗口
- MySQL常用命令行
- 一张图理解prototype、proto和constructor的三角关系
- 微信小程序把玩(三十七)location API
- hdu4258Covered Walkway(斜率)
- MVC简介及优缺点