package com.ruoyi.common.utils; 
 | 
  
 | 
import com.alibaba.fastjson.JSONObject; 
 | 
import com.ruoyi.common.constant.HttpStatus; 
 | 
import org.apache.commons.httpclient.methods.PostMethod; 
 | 
import org.apache.commons.httpclient.methods.RequestEntity; 
 | 
import org.apache.commons.httpclient.methods.StringRequestEntity; 
 | 
import org.apache.http.HttpResponse; 
 | 
import org.apache.http.client.methods.HttpPost; 
 | 
import org.apache.http.entity.StringEntity; 
 | 
import org.apache.http.entity.mime.HttpMultipartMode; 
 | 
import org.apache.http.entity.mime.MultipartEntity; 
 | 
import org.apache.http.entity.mime.content.FileBody; 
 | 
import org.apache.http.impl.client.CloseableHttpClient; 
 | 
import org.apache.http.impl.client.DefaultHttpClient; 
 | 
import org.apache.http.impl.client.HttpClients; 
 | 
import org.apache.http.message.BasicHeader; 
 | 
import org.apache.http.protocol.HTTP; 
 | 
  
 | 
import java.io.BufferedReader; 
 | 
import java.io.File; 
 | 
import java.io.InputStream; 
 | 
import java.io.InputStreamReader; 
 | 
import java.nio.charset.Charset; 
 | 
  
 | 
public class HttpClientKit { 
 | 
  
 | 
    public static String post(String url, JSONObject json) { 
 | 
  
 | 
        CloseableHttpClient client = HttpClients.createDefault(); 
 | 
        HttpPost post = new HttpPost(url); 
 | 
  
 | 
        post.setHeader("Content-Type", "application/json"); 
 | 
        post.addHeader("Authorization", "Basic YWRtaW46"); 
 | 
        String result = ""; 
 | 
  
 | 
        try { 
 | 
  
 | 
            StringEntity s = new StringEntity(json.toString(), "utf-8"); 
 | 
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
 | 
            post.setEntity(s); 
 | 
  
 | 
            // 发送请求 
 | 
            HttpResponse httpResponse = client.execute(post); 
 | 
  
 | 
            // 获取响应输入流 
 | 
            InputStream inStream = httpResponse.getEntity().getContent(); 
 | 
            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"); 
 | 
            inStream.close(); 
 | 
  
 | 
            result = strber.toString(); 
 | 
            System.out.println(result); 
 | 
  
 | 
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SUCCESS) { 
 | 
                // System.out.println("请求服务器成功,做相应处理"); 
 | 
            } else { 
 | 
                System.out.println("请求服务端失败"); 
 | 
            } 
 | 
        } catch (Exception e) { 
 | 
            System.out.println("请求异常"); 
 | 
            throw new RuntimeException(e); 
 | 
        } finally { 
 | 
            //client. 
 | 
        } 
 | 
        return result; 
 | 
    } 
 | 
  
 | 
    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.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 + ";charset=UTF-8"); 
 | 
  
 | 
        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); 
 | 
        } 
 | 
        return new String(buffer.toString().getBytes(), "UTF-8"); 
 | 
    } 
 | 
  
 | 
    //    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"); 
 | 
  
 | 
        try { 
 | 
            RequestEntity entity = new StringRequestEntity(json, "Content-Type", "UTF-8"); 
 | 
            httpPost.setRequestEntity(entity); 
 | 
            httpClient.executeMethod(httpPost); 
 | 
  
 | 
            return httpPost.getResponseBodyAsString(); 
 | 
        } catch (Exception e) { 
 | 
            e.printStackTrace(); 
 | 
        } 
 | 
        return ""; 
 | 
    } 
 | 
  
 | 
    public static String postMsg(String url, JSONObject json) { 
 | 
  
 | 
        CloseableHttpClient client = HttpClients.createDefault(); 
 | 
        HttpPost post = new HttpPost(url); 
 | 
  
 | 
        post.setHeader("Content-Type", "application/json"); 
 | 
        //post.addHeader("Authorization", "Basic YWRtaW46"); 
 | 
        //post.addHeader("ApiKey", "hswl"); 
 | 
        post.addHeader("PostMethod", "POST"); 
 | 
        String result = ""; 
 | 
  
 | 
        try { 
 | 
  
 | 
            StringEntity s = new StringEntity(json.toJSONString(), "utf-8"); 
 | 
            s.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
 | 
            post.setEntity(s); 
 | 
  
 | 
            // 发送请求 
 | 
            HttpResponse httpResponse = client.execute(post); 
 | 
  
 | 
            // 获取响应输入流 
 | 
            InputStream inStream = httpResponse.getEntity().getContent(); 
 | 
            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"); 
 | 
            inStream.close(); 
 | 
  
 | 
            result = strber.toString(); 
 | 
            System.out.println(result); 
 | 
  
 | 
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SUCCESS) { 
 | 
                //System.out.println("请求服务器成功,做相应处理"); 
 | 
            } else { 
 | 
                System.out.println("请求服务端失败"); 
 | 
            } 
 | 
        } catch (Exception e) { 
 | 
            System.out.println("请求异常"); 
 | 
            throw new RuntimeException(e); 
 | 
        } finally { 
 | 
            //client. 
 | 
        } 
 | 
        return result; 
 | 
    } 
 | 
} 
 |