Http发送单条数据和发送多条数据

此处使用的是springboot

  1. 单条数据

    服务端:

    @RequestMapping(value="/sentimentAnalysisPost",method = RequestMethod.POST)
        public String getSentimentAnalysisResultPost1(@RequestBody String text){
            if(text != null){
                String context = Dataprocessing.WordSegmentation(text);
                int a = SentimentAnalysisModel.getSentimentAnalysisModel().sentimentRnn(context);
                return String.valueOf(a);
            }
            return "";
        }

    客户端:
     

    public static String PostRequest(String URL,String context) {
            String jsonString="";
            try {
                //创建连接
                URL url = new URL(URL);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setRequestMethod("POST"); //设置请求方法
                connection.setRequestProperty("Charset", "UTF-8"); //设置请求编码
                connection.setUseCaches(false);
                connection.setInstanceFollowRedirects(true);
                connection.setRequestProperty("Content-Type", "application/application/x-www-form-urlencoded");
                connection.connect();
                //POST请求
                PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(),"utf-8"));
                out.write(context);
                out.flush();
                out.close();
                // 读取响应
                if (connection.getResponseCode()==200) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String lines;
                    StringBuilder sb = new StringBuilder();
                    while ((lines = reader.readLine()) != null) {
                        lines = new String(lines.getBytes(), "utf-8");
                        sb.append(lines);
                    }
                    jsonString=sb.toString();
                    reader.close();
                }//返回值为200输出正确的响应信息
                if (connection.getResponseCode()==400) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
                    String lines;
                    StringBuilder sb = new StringBuilder();
                    while ((lines = reader.readLine()) != null) {
                        lines = new String(lines.getBytes(), "utf-8");
                        sb.append(lines);
                    }
                    jsonString=sb.toString();
                    reader.close();
                }//返回值错误,输出错误的返回信息
                // 断开连接
                connection.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return jsonString;
        }
    public static void main(String[] args){
        System.println.out(PostRequest("http://127.0.0.1:8080/sentimentAnalysisPost","你好"));
    }
  2. 批量数据
    服务端:这里可以填List也可以填Map,如果是List就是直接将Map<String, String> text 换成List<String> text
     

     @RequestMapping(value = "/sentimentAnalysisBatchPost", method = RequestMethod.POST)
        @ResponseBody
        public Map<String,String> getSentimentAnalysisResultPost2(@RequestBody Map<String,String> text) {
            Map<String,String> map = new HashMap<>();
            if (text.size() != 0) {
                for (String str : text.keySet()) {
                    String context = Dataprocessing.WordSegmentation(text.get(str));
                    int a = SentimentAnalysisModel.getSentimentAnalysisModel().sentimentRnn(context);
                    map.put(str,String.valueOf(a));
                }
            }
            return map;
        }

    客户端:
     

     public static String PostRequest(String URL,String title) {
            String jsonString="";
            try {
                //创建连接
                URL url = new URL(URL);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setRequestMethod("POST"); //设置请求方法
                connection.setRequestProperty("Charset", "UTF-8"); //设置请求编码
                connection.setUseCaches(false);
                connection.setInstanceFollowRedirects(true);
                connection.setRequestProperty("Content-Type", "application/json");
                connection.connect();
                //POST请求
                PrintWriter out = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(),"utf-8"));
                out.write(title);
                out.flush();
                out.close();
                // 读取响应
                if (connection.getResponseCode()==200) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                    String lines;
                    StringBuilder sb = new StringBuilder();
                    while ((lines = reader.readLine()) != null) {
                        lines = new String(lines.getBytes(), "utf-8");
                        sb.append(lines);
                    }
                    jsonString=sb.toString();
                    reader.close();
                }//返回值为200输出正确的响应信息
                if (connection.getResponseCode()==400) {
                    BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getErrorStream()));
                    String lines;
                    StringBuilder sb = new StringBuilder();
                    while ((lines = reader.readLine()) != null) {
                        lines = new String(lines.getBytes(), "utf-8");
                        sb.append(lines);
                    }
                    jsonString=sb.toString();
                    reader.close();
                }//返回值错误,输出错误的返回信息
                // 断开连接
                connection.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return jsonString;
        }
    public static void main(String[] args){
    //  String body = new JSONArray(new String[]{"你好","你不好"}).toString();
            Map<String,String> map = new HashMap<>();
            map.put("id1","你好");
            map.put("id2","你不好");
            JSONObject jsonObject = new JSONObject(map);
            String body2 = jsonObject.toString();
            System.out.println(PostRequest("http://127.0.0.1:8080/sentimentAnalysisBatchPost",body2));
    }

     

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值