本文记录的是使用HttpURLConnection发送Get和Post请求的示例, 关于HttpURLConnection的使用请参考: JDK中URLConnection使用详解.

这里将发送请求的方法封装在HttpRequest的类中,使用HttpURLConnection发送Http的Get和Post请求需要引入下面的类:

1
2
3
4
5
6
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

其中, 发送Get请求的方法如下:

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
public static String sendGet(String url) {
String responseBody = null;
try {
URL getUrl = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection)getUrl
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();

int respCode = urlConnection.getResponseCode();
if (HttpURLConnection.HTTP_OK == respCode) {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
String readLine = null;
StringBuffer response = new StringBuffer();
while (null != (readLine = bufferedReader.readLine())) {
response.append(readLine);
}

bufferedReader.close();
responseBody = response.toString();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return responseBody;
}

其中传入的参数String url是发送Get请求的URL,若发送带请求参数的Get请求,那么该url是已拼接好的带请求参数的字符串。

发送Post请求的方法如下:

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
public static String sendPost(String url, String params) {
String responseBody = null;
try {
URL postUrl = new URL(url);
HttpURLConnection urlConnection = (HttpURLConnection)postUrl
.openConnection();
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded;charset=utf-8");
urlConnection.setRequestProperty("Content-Length",
String.valueOf(params.length()));
urlConnection.setDoOutput(true);
urlConnection.setUseCaches(false);
urlConnection.setConnectTimeout(30000);
urlConnection.setReadTimeout(30000);
urlConnection.connect();

DataOutputStream outputStream = new DataOutputStream(
urlConnection.getOutputStream());
outputStream.writeBytes(params);
outputStream.flush();
outputStream.close();

int respCode = urlConnection.getResponseCode();
if (HttpURLConnection.HTTP_OK == respCode) {
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(urlConnection.getInputStream(),
"UTF-8"));
String inputLine = null;
StringBuffer response = new StringBuffer();
while (null != (inputLine = bufferedReader.readLine())) {
response.append(inputLine);
}

bufferedReader.close();
responseBody = response.toString();
} else {
//TODO:
}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return responseBody;
}

可使用如下的方式调用上面的sendGet和sendPost方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static void main(String[] agrs) {
String urlString = "http://localhost:8080/HttpRequestProcessor/weixin";
try {
String params = URLEncoder.encode("vobser", "UTF-8");
String getURL = urlString + "?action=sendGet&params="+params;
String response = HttpRequest.sendGet(getURL);
System.out.println("get response = " + response);

String postUrl = urlString;
StringBuffer paramsBuffer = new StringBuffer();
paramsBuffer.append("action=");
//使用URLEncoder.encode对特殊和不可见字符进行编码
paramsBuffer.append(URLEncoder.encode("get info", "UTF-8"));
paramsBuffer.append("&param=");
paramsBuffer.append(URLEncoder.encode("云竹", "UTF-8"));
response = HttpRequest.sendPost(postUrl, paramsBuffer.toString());
System.out.println("post response = " + response);
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

目前已转行教育行业,欢迎加微信交流:CaryaLiu