liusheng
2023-08-16 af395123e74c61c8aa33bc7d2c48a3528d65f887
ruoyi-common/src/main/java/com/ruoyi/common/utils/HttpClientKit.java
@@ -37,8 +37,7 @@
        try {
            StringEntity s = new StringEntity(json.toString(), "utf-8");
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            post.setEntity(s);
            // 发送请求
@@ -46,12 +45,10 @@
            // 获取响应输入流
            InputStream inStream = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inStream, "utf-8"));
            BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
            StringBuilder strber = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null)
                strber.append(line + "\n");
            while ((line = reader.readLine()) != null) strber.append(line + "\n");
            inStream.close();
            result = strber.toString();
@@ -65,8 +62,7 @@
        } catch (Exception e) {
            System.out.println("请求异常");
            throw new RuntimeException(e);
        }
        finally {
        } finally {
            //client.
        }
        return result;
@@ -74,11 +70,11 @@
    public static String sendPostWithFile(File lrcFile, String urlStr) throws Exception {
        String BOUNDARY = java.util.UUID.randomUUID().toString();
        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--------------------" + BOUNDARY, Charset.defaultCharset());
        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--------------------" + BOUNDARY, Charset.forName("UTF-8"));
        multipartEntity.addPart("lyricsUpload", new FileBody(lrcFile));    //lyricsUpload文件名
        HttpPost request = new HttpPost(urlStr);    //远程接口
        request.setEntity(multipartEntity);
        request.addHeader("Content-Type", "multipart/form-data; boundary=--------------------" + BOUNDARY);
        request.addHeader("Content-Type", "multipart/form-data; boundary=--------------------" + BOUNDARY + ";charset=UTF-8");
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);
@@ -89,32 +85,49 @@
        while ((line = in.readLine()) != null) {
            buffer.append(line);
        }
        //Flogger.info("发送消息收到的返回:" + buffer.toString());
        String remark = "";
        return  buffer.toString();
       // org.json.JSONObject JSONObject = new org.json.JSONObject(buffer.toString());
        return new String(buffer.toString().getBytes(), "UTF-8");
    }
    public  static  String postOpr(String url,String json)
    {
    //    public static String sendPostWithFile(File lrcFile, String urlStr) throws Exception {
//        String BOUNDARY = java.util.UUID.randomUUID().toString();
//        MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, "--------------------" + BOUNDARY, Charset.defaultCharset());
//        multipartEntity.addPart("lyricsUpload", new FileBody(lrcFile));    //lyricsUpload文件名
//        HttpPost request = new HttpPost(urlStr);    //远程接口
//        request.setEntity(multipartEntity);
//        request.addHeader("Content-Type", "multipart/form-data; boundary=--------------------" + BOUNDARY);
//
//        DefaultHttpClient httpClient = new DefaultHttpClient();
//        HttpResponse response = httpClient.execute(request);
//        InputStream is = response.getEntity().getContent();
//        BufferedReader in = new BufferedReader(new InputStreamReader(is));
//        StringBuffer buffer = new StringBuffer();
//        String line = "";
//        while ((line = in.readLine()) != null) {
//            buffer.append(line);
//        }
//        //Flogger.info("发送消息收到的返回:" + buffer.toString());
//        String remark = "";
//        return  buffer.toString();
//       // org.json.JSONObject JSONObject = new org.json.JSONObject(buffer.toString());
//    }
    public static String postOpr(String url, String json) {
        //HttpClient httpClient = new HttpClient();
        org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
        PostMethod httpPost = new PostMethod(url);
        httpPost.setRequestHeader("Content-Type","application/json;charset=utf-8");
        httpPost.setRequestHeader("Content-Type", "application/json;charset=utf-8");
        try
        {
            RequestEntity entity = new StringRequestEntity(json,"Content-Type","UTF-8");
        try {
            RequestEntity entity = new StringRequestEntity(json, "Content-Type", "UTF-8");
            httpPost.setRequestEntity(entity);
            httpClient.executeMethod(httpPost);
            return  httpPost.getResponseBodyAsString();
        }
        catch (Exception e)
        {
            return httpPost.getResponseBodyAsString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  "";
        return "";
    }
    public static String postMsg(String url, JSONObject json) {
        CloseableHttpClient client = HttpClients.createDefault();
@@ -129,8 +142,7 @@
        try {
            StringEntity s = new StringEntity(json.toJSONString(), "utf-8");
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
                    "application/json"));
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
            post.setEntity(s);
            // 发送请求
@@ -138,12 +150,10 @@
            // 获取响应输入流
            InputStream inStream = httpResponse.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    inStream, "utf-8"));
            BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "utf-8"));
            StringBuilder strber = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null)
                strber.append(line + "\n");
            while ((line = reader.readLine()) != null) strber.append(line + "\n");
            inStream.close();
            result = strber.toString();
@@ -157,8 +167,7 @@
        } catch (Exception e) {
            System.out.println("请求异常");
            throw new RuntimeException(e);
        }
        finally {
        } finally {
            //client.
        }
        return result;