0%

HttpURLConnection

阅读更多

1 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
public static void doGet(String value1, String value2, String operator) {
try {
URL url = new URL("http://localhost:8080/compute?value1=" + value1 + "&value2=" + value2);//(1)

HttpURLConnection conn = (HttpURLConnection) url.openConnection();//(2)
conn.setRequestMethod("GET");//(3)
conn.setRequestProperty("operator", operator);//(4)
conn.setDoInput(true);//(5)

conn.connect();//(6)

BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));//(7)
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (MalformedURLException e) {
System.err.println("URL初始化错误");
e.printStackTrace(System.err);
} catch (IOException e) {
System.err.println("数据流错误");
e.printStackTrace(System.err);
}
}

详细步骤分析

  1. 创建URL对象,将String类型的URL转为URL类型
  2. 利用URL对象创建URLConnection对象,并转型为HttpURLConnection
  3. 设置方法类型为GET,可以不写,默认方法就是GET
  4. 设置自定义的Header键值对
  5. 打开输入流,用于读取返回的响应数据,默认打开,可以不写
  6. 进行连接,也可以不写,getInputStream()/getOutputStream()等操作会隐式进行连接操作
  7. 获取输入流,用于读取返回的响应数据

2 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
public static void doPost() {
try {
URL url = new URL("http://localhost:8080/login");//(1)

HttpURLConnection conn = (HttpURLConnection) url.openConnection();//(2)
conn.setRequestMethod("POST");//(3)
conn.setRequestProperty("Content-Type", "application/json");//(4)
conn.setDoInput(true);//(5)
conn.setDoOutput(true);//(5)

conn.connect();//(6)

String requestBody = "{\"name\":\"张三\",\"password\":\"123456789\"}";
PrintWriter out = new PrintWriter(conn.getOutputStream());
out.print(requestBody);//(7)
out.flush();//(8)

BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));//(9)
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (MalformedURLException e) {
System.err.println("URL初始化错误");
e.printStackTrace(System.err);
} catch (IOException e) {
System.err.println("数据流错误");
e.printStackTrace(System.err);
}
}

详细步骤分析

  1. 创建URL对象,将String类型的URL转为URL类型
  2. 利用URL对象创建URLConnection对象,并转型为HttpURLConnection
  3. 设置方法类型为POST,不可省略,因为默认的方法是GET
  4. 设置Header,指定Content(RequestBody)的格式为JSON
  5. 打开输入流,以及输出流。输出流用于写入RequestBody,必须写,默认关闭;输入流用于读取返回的响应数据,默认打开,可以不写
  6. 进行连接,也可以不写,getInputStream()/getOutputStream()等操作会隐式进行连接操作
  7. 通过getOutputStream()获取输出流,传入RequestBody
  8. 刷新写缓冲区,将RequestBody传送到到Server端
  9. 获取输入流,用于读取返回的响应数据

3 HttpURLConnection默认参数

代码清单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class DefaultParameters {
public static void main(String[] args) {
try {
URL url = new URL("http://localhost:8080/home");

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

System.out.println("Default RequestMethod: " + conn.getRequestMethod());
System.out.println("Default doInput: " + conn.getDoInput());
System.out.println("Default doOutput: " + conn.getDoOutput());

} catch (MalformedURLException e) {
System.err.println("URL初始化错误");
e.printStackTrace(System.err);
} catch (IOException e) {
System.err.println("数据流错误");
e.printStackTrace(System.err);
}
}
}

输出如下

1
2
3
Default RequestMethod: GET
Default doInput: true
Default doOutput: false

4 HttpURLConnection继承结构

HttpURLConnection