KKSU
2024-11-21 80fd9e3da4b45285c29cdb380ce743202b0c2af4
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
package com.best.javaSdk;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Map;
 
public class HttpService {
    public static String sendPost(String url, Map<String, String> paramMap, Map<String, String> headerMap) throws Exception {
        StringBuffer buffer = new StringBuffer(); //用来拼接参数
        StringBuffer result = new StringBuffer(); //用来接受返回值
        //创建URL
        URL httpUrl = new URL(url);
        //建立连接
        URLConnection connection = httpUrl.openConnection();
        connection.setRequestProperty("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        connection.setRequestProperty("connection", "keep-alive");
        connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        if (headerMap != null && !headerMap.isEmpty()) {
            for (Map.Entry<String, String> entry : headerMap.entrySet()) {
                connection.setRequestProperty(entry.getKey(),  URLEncoder.encode(entry.getValue(), "utf-8"));
            }
        }
 
        PrintWriter printWriter = new PrintWriter(connection.getOutputStream());
        String request = "";
        if(paramMap != null && !paramMap.isEmpty()){
            for (Map.Entry<String, String> entry : paramMap.entrySet()) {
                buffer.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "utf-8")).append("&");
            }
            //去掉最后一个&并urlencode
            request = buffer.toString().substring(0, buffer.toString().length() - 1);
        }
        printWriter.print(request);
        printWriter.flush();
        connection.connect();
        //接受连接返回参数
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            result.append(line);
        }
        bufferedReader.close();
        return result.toString();
    }
 
    public static String sendPost(String url, Map<String, String> paramMap, Map<String, String> headerMap, String body) throws Exception {
        StringBuffer buffer = new StringBuffer(); //用来拼接参数
        StringBuffer result = new StringBuffer(); //用来接受返回值
        String request = "";
        if(paramMap != null && !paramMap.isEmpty()){
            for (Map.Entry<String, String> entry : paramMap.entrySet()) {
                buffer.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "utf-8")).append("&");
            }
            //去掉最后一个&并urlencode
            request = buffer.toString().substring(0, buffer.toString().length() - 1);
        }
 
        //创建URL
        URL httpUrl = new URL(url + "?" + request);
        //建立连接
        URLConnection connection =  httpUrl.openConnection();
        connection.setRequestProperty("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
        connection.setRequestProperty("connection", "keep-alive");
        connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:34.0) Gecko/20100101 Firefox/34.0");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        if (headerMap != null && !headerMap.isEmpty()) {
            for (Map.Entry<String, String> entry : headerMap.entrySet()) {
                connection.setRequestProperty(entry.getKey(),  URLEncoder.encode(entry.getValue(), "utf-8"));
            }
        }
 
        PrintWriter printWriter = new PrintWriter(connection.getOutputStream());
 
        printWriter.print(body);
        printWriter.flush();
        connection.connect();
        //接受连接返回参数
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            result.append(line);
        }
        bufferedReader.close();
        return result.toString();
    }
 
}