场景:后台Http请求其它接口获取token

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
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
private String HttpGetUrlJump(String getUrl, Map<String, Object> HeaderParams, Map<String, Object> params) {
String result = "";
try {
//拼url
if (params != null) {
Iterator<String> it = params.keySet().iterator();
StringBuffer sb = null;
while (it.hasNext()) {
String key = it.next();
String value = (String) params.get(key);
if (sb == null) {
sb = new StringBuffer();
sb.append("?");
} else {
sb.append("&");
}
sb.append(key);
sb.append("=");
sb.append(value);
}
getUrl += sb.toString();
}

//创建url
URL url = new URL(getUrl);
//创建连接对象(HttpURLConnection是实现的接口URLConnection)
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//默认为true,设置后可使用inputStream获取数据
connection.setDoInput(true);
//设置为GET提交
connection.setRequestMethod("GET");
//Post 请求不能使用缓存
connection.setUseCaches(false);
//最高超时时间
connection.setConnectTimeout(60000);
//最高读取时间
connection.setReadTimeout(60000);
//最高连接时间
connection.setConnectTimeout(60000);
//设置本次连接是否自动重定向
connection.setInstanceFollowRedirects(false);

//判断是否需要Header传参
if (HeaderParams.size() > 0) {
Set<String> set = HeaderParams.keySet();
for (String str : set) {
//设置每一条请求头信息
connection.setRequestProperty(str, (String) HeaderParams.get(str));
}
}
//连接
connection.connect();

//获取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
result += line;
}
//关闭reader
reader.close();
//断开连接
connection.disconnect();
return result;
} catch (Exception e) {
e.printStackTrace();
return result;
}
}

上面拼url方法来自网络,非常巧妙,使用一个外接变量,初始设为null判断为null为第一次的方式巧妙地确定了第一次———————good,good,good,机智,机智,机智

POST请求

需要传入url,请求头header,请求body信息(都以map的形式传入)

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
private String HttpPostUrlJump(String postUrl, Map<String,Object> HeaderParams, Map<String,Object> params){
String result="";
try {
//创建url
URL url = new URL(postUrl);
//创建连接对象(HttpURLConnection是实现的接口URLConnection)
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//默认未false,设置后可使用outputStream传数据;若为get请求不需开启
connection.setDoOutput(true);
//默认为true,设置后可使用inputStream获取数据
connection.setDoInput(true);
//设置为post提交
connection.setRequestMethod("POST");
//Post 请求不能使用缓存
connection.setUseCaches(false);
//最高超时时间
connection.setConnectTimeout(60000);
//最高读取时间
connection.setReadTimeout(60000);
//最高连接时间
connection.setConnectTimeout(60000);
//设置本次连接是否自动重定向
connection.setInstanceFollowRedirects(false);
//设置请求头信息key,value
//Content-Type设置发送的内容编码类型,通用使用application/x-www-form-urlencoded,json使用application/json
connection.setRequestProperty("Content-Type","application/json");

//判断是否需要Header传参
if(HeaderParams.size()>0){
Set<String> set = HeaderParams.keySet();
for (String str : set) {
//设置每一条请求头信息
connection.setRequestProperty(str, (String) HeaderParams.get(str));
}
}

//连接
connection.connect();
// 得到请求的输出流对象
OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(),"UTF-8");
//传入请求体信息body为json格式
if(params.size()>0){
//传入body内容json格式
JSONObject json = new JSONObject(params);
writer.write(String.valueOf(json));
}else{
//传入参数为空时传入{}
writer.write("{}");
}
//刷新缓冲区
writer.flush();
//关闭writer
writer.close();

//获取响应
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null){
result += line;
}
//关闭reader
reader.close();
//断开连接
connection.disconnect();
return result;
} catch (Exception e) {
e.printStackTrace();
return result;
}

注:其中有些值还是写死,具体情况具体修改

Http的两种提交方式跳转:GET,POST比较