HttpUtil.java示例代码[Java]

Java HTTP请求通用函数
作者:xiezhongpian 阅读数:16443 上传时间:2017-05-08

HttpUtil.java

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
  package api.util;
 
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
 
import javax.net.ssl.HttpsURLConnection;
 
public class HttpUtil {
    private static String USER_AGENT = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; customie8)";
 
    // HTTP GET request
    public static String sendGet(String url, String charset) throws Exception {
        URL realurl = new URL(url);
        HttpURLConnection con = (HttpURLConnection) realurl.openConnection();
 
        // optional default is GET
        con.setRequestMethod("GET");
 
        // add request header
        con.setRequestProperty("User-Agent", USER_AGENT);
 
        // int responseCode = con.getResponseCode();
        // System.out.println("Response Code : " + responseCode);
 
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
        String inputLine;
        StringBuffer result = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            result.append(inputLine);
            result.append("\r\n");
        }
        in.close();
        con.disconnect();
        return result.toString();
    }
 
    // HTTP POST request
    @SuppressWarnings("deprecation")
    public static String sendPost(String url, Map<String, String> param, String charset) throws Exception {
        URL realurl = new URL(url);
        HttpsURLConnection con = (HttpsURLConnection) realurl.openConnection();
         
        con.setRequestMethod("POST");
        // add reuqest header
        con.setRequestProperty("User-Agent", USER_AGENT);
        // con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
 
        // String urlParameters =
        // "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
        StringBuffer buffer = new StringBuffer();
        if (param != null && !param.isEmpty()) {
            for (Map.Entry<String, String> entry : param.entrySet()) {
                buffer.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), charset))
                        .append("&");
            }
        }
        buffer.deleteCharAt(buffer.length() - 1);
        // Send post request
        con.setDoOutput(true);
        DataOutputStream wr = new DataOutputStream(con.getOutputStream());
        wr.writeBytes(buffer.toString());
        wr.flush();
        wr.close();
        // int responseCode = con.getResponseCode();
        // System.out.println("Post parameters : " + urlParameters);
        // System.out.println("Response Code : " + responseCode);
 
        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream(), charset));
        String inputLine;
        StringBuffer result = new StringBuffer();
        while ((inputLine = in.readLine()) != null) {
            result.append(inputLine);
            result.append("\r\n");
        }
        in.close();
        con.disconnect();
        return result.toString();
    }
}
×