HttpURLConnection的Psot请求

遇到问题的解决方案

贴出地址:我的问题的解决方案地址

public static String requestPost(JSONObject object) {
        try {
            String baseUrl = "http://127.0.0.1:8080/xxx";
            String params = object.toString();
            byte[] postData = params.getBytes();
            URL url = new URL(baseUrl);
            HttpURLConnection urlConn = (HttpURLConnection)url.openConnection();
            urlConn.setConnectTimeout(5000);
            urlConn.setReadTimeout(5000);
            urlConn.setDoOutput(true);
            urlConn.setDoInput(true);
            urlConn.setUseCaches(false);
            urlConn.setRequestMethod("POST");
            urlConn.setInstanceFollowRedirects(true);
            urlConn.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
            urlConn.connect();
            DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());
            dos.write(postData);
            dos.flush();
            dos.close();
            StringBuilder builder = new StringBuilder();
            if (urlConn.getResponseCode() == 200) {
                BufferedReader reader = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

                String line;
                while((line = reader.readLine()) != null) {
                    builder.append(line);
                }

                return builder.toString();
            }

            urlConn.disconnect();
        } catch (Exception var10) {
        }

        return null;
    }```

你可能感兴趣的:(后端)