详细参考这个博文:http://www.cnblogs.com/itliucheng/p/5065619.html
一、get请求:
//关键代码就这几行
String urlNameString ="http://127.0.0.1:8601/setResult/" + text + "/" + newWordsText;
// //往服务器端写内容 也就是发起http请求需要带的参数
// // 根据地址获取请求
HttpGet request = new HttpGet(urlNameString);//这里发送get请求
// // 获取当前客户端对象
HttpClient httpClient = new DefaultHttpClient();
// // 通过请求对象获取响应对象
HttpResponse response = httpClient.execute(request);
======================================================================
@RequestMapping("getWeChatUserInfo") @ResponseBody public String getWeChatUserInfo(String token,String openid){ String urlNameString = "https://api.weixin.qq.com/sns/userinfo?access_token=TOKEN&openid=OPENID"; urlNameString=urlNameString.replace("TOKEN", token); urlNameString=urlNameString.replace("OPENID",openid); String result=""; try { // 根据地址获取请求 HttpGet request = new HttpGet(urlNameString);//这里发送get请求 // 获取当前客户端对象 HttpClient httpClient = new DefaultHttpClient(); // 通过请求对象获取响应对象 HttpResponse response = httpClient.execute(request); // 判断网络连接状态码是否正常(0--200都数正常) if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { result= EntityUtils.toString(response.getEntity(),"utf-8"); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; //....result是用户信息,站内业务以及具体的json转换这里也不写了... }
//要调用的接口形式 @GetMapping("/setResult/{word}/{classifyName}") public Result<String> setResult(@PathVariable("word") String word, @PathVariable("classifyName") String classifyName) { int i1 = classifyService.selectClassifyCountByName(classifyName);
二、但是由于get方式请求内容大小有限,现在改由post请求提交:
//接口形式 // @ApiOperation("测试选择数据并添加分类接口,word需要分类的文本,classifyName添加分类") @PostMapping("/setResult") public Result<String> setResult(@RequestParam String word,@RequestParam String classifyName) { int i1 = classifyService.selectClassifyCountByName(classifyName); if (i1 > 0) { //分类已存在 Classify classify = classifyService.selectClassifyByName(classifyName); KeyWord keyword = new KeyWord(); keyword.setKeywordName(word); keyword.setClassifyId(classify.getId()); classifyService.addKeyWord(keyword);//添加KeyWord数据 modelExercise();//更新本地硬盘的表
//关键代码就这几行
String urlNameString = "http://127.0.0.1:8601/setResult"; //POST的URL HttpPost httppost = new HttpPost(urlNameString); //建立HttpPost对象 List<NameValuePair> params = new ArrayList<NameValuePair>(); //建立一个NameValuePair数组,用于存储欲传送的参数 params.add(new BasicNameValuePair("word", word)); params.add(new BasicNameValuePair("classifyName", classifyName)); //添加参数 httppost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); //设置编码 HttpResponse response = new DefaultHttpClient().execute(httppost); //发送Post,并返回一个HttpResponse对象 if (response.getStatusLine().getStatusCode() == 200) {//如果状态码为200,就是正常返回 String result = EntityUtils.toString(response.getEntity()); }