JAVA HttpURLConnection Post方式提交传递参数

参考1:https://blog.csdn.net/w348399060/article/details/62424502
下面给出两个方法:

 /*分词方法1——获取分词列表*/
    public static List SegWord_method(String title) throws IOException {
        List wordList = new ArrayList();
        StringBuffer response = new StringBuffer();
        HttpURLConnection connection = null;
        try {
           
            String url = "http://c*******?";

            // 请求url
            URL postUrl = new URL(url);
            // 打开连接
            connection = (HttpURLConnection) postUrl.openConnection();
            connection.setDoOutput(true);
            connection.setDoInput(true);
            connection.setRequestMethod("POST");    // 默认是 GET方式
            connection.setUseCaches(false);         // Post 请求不能使用缓存
            connection.setInstanceFollowRedirects(true);   //设置本次连接是否自动重定向
            connection.setRequestProperty("User-Agent", "Mozilla/5.0");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            connection.addRequestProperty("Connection","Keep-Alive");//设置与服务器保持连接
            connection.setRequestProperty("Accept-Language", "zh-CN,zh;0.9");

            // 连接
            connection.connect();
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());

            // 正文,正文内容其实跟get的URL中 '? '后的参数字符串一致
            String content = "i=" + URLEncoder.encode(title, "utf-8");
            out.writeBytes(content);

            //流用完记得关
            out.flush();
            out.close();
            //获取响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            String line;
            while ((line = reader.readLine()) != null){
                response.append(line);
           //  System.out.println("debug--line:" + line);
            }
            reader.close();
            connection.disconnect();

            //将获取结果转化为列表
            JSONObject result = JSON.parseObject(response.toString());
            com.jd.fastjson.JSONArray data = result.getJSONArray("data");
            for (int i = 0; i < data.size(); i++) {
                JSONObject b = data.getJSONObject(i);
                String word = b.getString("word");
                //  System.out.println("word:" + word);
                wordList.add(word);
            }

        }catch (Exception e){
            logger.error("error");
            e.printStackTrace();
        }
        return wordList;
    }

    // 分词方法2——获取分词列表
    public static List SegWord_method2(String title) {

        String url = "http://cwss**************?i=" + title;
        List wordList = new ArrayList();
        StringBuilder response = new StringBuilder();
        try {
            URL oracle = new URL(url);
            URLConnection yc = oracle.openConnection();
            BufferedReader in = new BufferedReader(new InputStreamReader(
                    yc.getInputStream()));

            String inputLine = null;
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();

            //将获取json字符串response转化为需要的列表
            JSONObject result = JSON.parseObject(response.toString());
            com.jd.fastjson.JSONArray data = result.getJSONArray("data");

            for (int i = 0; i < data.size(); i++) {
                JSONObject b = data.getJSONObject(i);
                String word = b.getString("word"); //每个词
                wordList.add(word);
            }

        } catch (IOException e) {
            logger.error("error");
            e.printStackTrace();
        }
        return wordList;
    }

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,539评论 25 709
  • 用两张图告诉你,为什么你的 App 会卡顿? - Android - 掘金 Cover 有什么料? 从这篇文章中你...
    hw1212阅读 13,125评论 2 59
  • 在此特此声明:一下所有链接均来自互联网,在此记录下我的查阅学习历程,感谢各位原创作者的无私奉献 ! 技术一点一点积...
    远航的移动开发历程阅读 11,381评论 12 197
  • 通常我们创建的文件(UIViewController为例)默认是这样的 但是我们通常都有自己的代码规范 比如#pr...
    JaiUnChat阅读 802评论 0 1
  • 大多数人看完这部电视剧,可能都会被闫学晶扮演的蓝月这个角色所感动,可我却心情沉重。一个女人背负着家庭所有的责任,维...
    赵乙默阅读 249评论 0 0