HttpClientUtil.java示例代码[Java]

Java Android请求通用函数
作者:xiezhongpian 阅读数:12605 上传时间:2017-05-09

HttpClientUtil.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
  package api.util;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
 
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
 
public class HttpClientUtil {
    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 {
        CloseableHttpClient client = null;
        StringBuffer result = null;
        try {
            client = HttpClients.createDefault();
            HttpGet request = new HttpGet(url);
 
            // add request header
            request.addHeader("User-Agent", USER_AGENT);
 
            HttpResponse response = client.execute(request);
 
            // System.out.println("Response Code : " +
            // response.getStatusLine().getStatusCode());
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
 
            result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            client.close();
        }
        return result.toString();
    }
 
    // HTTP POST request
    public static String sendPost(String url, List<NameValuePair> param, String charset) throws Exception {
        CloseableHttpClient client = null;
        StringBuffer result = null;
        try {
            client = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);
 
            // add header
            post.setHeader("User-Agent", USER_AGENT);
            post.setEntity(new UrlEncodedFormEntity(param));
            HttpResponse response = client.execute(post);
            // System.out.println("Post parameters : " + post.getEntity());
            // System.out.println("Response Code : " +
            // response.getStatusLine().getStatusCode());
 
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), charset));
            result = new StringBuffer();
            String line = "";
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            return result.toString();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            client.close();
        }
        return result.toString();
    }
}
×