ChenJiaHe
2020-11-25 db7fc9f145beb76bbef19b1812d63d2ffc2d0df9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package com.hx.util;
 
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
public class RequestMethod {
 
    /**
     * 发送Post请求
     * @param path 请求路径
     * @param params 请求参数
     * @param encoding 编码
     * @return 服务器端内容
     */
    public static String sendPOSTRequest(String path, Map<String, String> params, String encoding) throws Exception{
        StringBuilder data = new StringBuilder();
        if(params!=null && !params.isEmpty()){
            for(Map.Entry<String, String> entry : params.entrySet()){
                data.append(entry.getKey()).append("=");
                data.append(URLEncoder.encode(entry.getValue(), encoding));
                data.append("&");
            }
            data.deleteCharAt(data.length() - 1);
        }
        byte[] entity = data.toString().getBytes();//生成实体数据
        HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);//允许对外输出数据
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
        OutputStream outStream = conn.getOutputStream();
        outStream.write(entity);
        if(conn.getResponseCode() == 200){
            return StreamUtils.InputStreamTOString(conn.getInputStream()) ;
        }
        return null;
    }
    
    /**
     * 发送Post请求
     * @param path 请求路径
     * @param content 发送内容
     * @param encoding 编码
     * @return 服务器端内容
     */
    public static String sendPOSTRequest(String path, String content, String encoding){
        //byte[] entity = content.toString().getBytes();//生成实体数据
        try{
            HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
            conn.setConnectTimeout(5000);
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);//允许对外输出数据
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setRequestProperty("Charset", "utf-8");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            //conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
            OutputStreamWriter outStream = new OutputStreamWriter(conn.getOutputStream(),"UTF-8");
            outStream.write(content);
            outStream.flush();
            if(conn.getResponseCode() == 200){
                return StreamUtils.InputStreamTOString(conn.getInputStream());
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 发送Post请求
     * @param path 请求路径
     * @param entity 发送内容
     * @param encoding 编码
     * @return 服务器端内容
     */
    public static String sendPOSTRequest(String path, byte[] entity, String encoding) throws Exception{
        System.out.println("entity:" + entity.length);
        HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("POST");
        conn.setDoOutput(true);//允许对外输出数据
        conn.setRequestProperty("Content-Type", "application/octet-stream");
        conn.setRequestProperty("Accept", "application/octet-stream");
        conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
        OutputStream outStream = conn.getOutputStream();
        outStream.write(entity);
        if(conn.getResponseCode() == 200){
            return StreamUtils.InputStreamTOString(conn.getInputStream());
        }
        return null;
    }
    
    /**
     * 发送GET请求
     * @param path 请求路径
     * @param params 请求参数
     * @return 服务器端内容
     */
    public static String sendGETRequest(String path, Map<String, String> params, String ecoding) throws Exception{
        // http://192.168.1.100:8080/web/ManageServlet?title=xxx&timelength=90
        StringBuilder url = new StringBuilder(path);
        if(params!=null){
            url.append("?");
                for(Map.Entry<String, String> entry : params.entrySet()){
                    url.append(entry.getKey()).append("=");
                    url.append(URLEncoder.encode(entry.getValue(), ecoding));
                    url.append("&");
                }
            url.deleteCharAt(url.length() - 1);
        }
        HttpURLConnection conn = (HttpURLConnection)new URL(url.toString()).openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        if(conn.getResponseCode() == 200){
            return StreamUtils.InputStreamTOString(conn.getInputStream()) ;
        }
        return null;
    }
    
    
    
    public static String msg(InputStream in) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String line;
        List<String> lines  = new ArrayList<String>();
        while((line = br.readLine())!=null){
            lines.add(line);
        }
        if(lines.size()>0){
            String f = lines.get(0);
            if(f.indexOf("200")!=-1){
                String e = lines.get(lines.size()-1);
                return e;
            }else{
                return null;
            }
        }else{
            return null;
        }
    }
}