zoukankan      html  css  js  c++  java
  • HttpClient远程调用接口

      详细参考这个博文: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()); }

    三、结果数据库用varchar无法容纳内容,现在改由text来存储

  • 相关阅读:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'getDataSource' defined in class path resource nested exception is org.springframework.beans.factory.BeanCreat异常
    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating Caused by: tk.mybatis.mapper.MapperException: 无法获取 cn.itcast.mapper.UserMapper.existsWithPrimaryKey 方法的泛型信息!
    fibnacci数列递归实现
    XOR加密
    Pep8课下作业
    求最大公约数伪代码
    《信息安全专业导论》第五周学习总结
    20191306《信息安全专业导论》第四周学习总结
    用或非门实现其他逻辑门
    寻找黑客偶像
  • 原文地址:https://www.cnblogs.com/xiaohouzai/p/8980498.html
Copyright © 2011-2022 走看看