-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequest.java
More file actions
93 lines (80 loc) · 2.92 KB
/
HttpRequest.java
File metadata and controls
93 lines (80 loc) · 2.92 KB
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
85
86
87
88
89
90
91
92
93
package jmeterClass;
/**
* Created by xglh on 2017-7-21.
*/
import java.io.*;
import java.net.*;
import java.util.List;
import java.util.Map;
public class HttpRequest {
//方法重写,实现headers默认为空
public static String sendHead(String url,String host){
String[][] headers = {};
return HttpRequest.sendHead(url,host,headers);
}
/**
* 发送http head请求,只接受响应头,不接受响应内容
* @param url 请求的url
* @param host 代理,支持形如 192.168.2.235 192.168.2.2.235:8083的形式,为""表示不设代理
* @param headers 请求头,二维String数组形式
* @return
*/
public static String sendHead(String url,String host,String[][] headers) {
String result = "";
BufferedReader in = null;
String code = "";
try {
URL realUrl = new URL(url);
URLConnection connection;
String hostname = "";
int port = 0;
if(host != ""){
hostname = host;
port = 80;
if (host.indexOf(":") >= 0){
hostname = host.substring(0,host.indexOf(":"));
port = Integer.parseInt(host.substring(host.indexOf(":") + 1,host.length()));
}
InetSocketAddress addr = new InetSocketAddress(hostname,port);
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); // http 代理
connection = realUrl.openConnection(proxy);
}
else{
connection = realUrl.openConnection();
}
// 打开和URL之间的连接
HttpURLConnection httpUrlConnection = (HttpURLConnection) connection;
//发送head请求,不接受返回内容,只接受响应头
httpUrlConnection.setRequestMethod("HEAD");
// 设置请求头
for(int i = 0;i<headers.length;i++){
connection.setRequestProperty(headers[i][0], headers[i][1]);
}
// 建立实际的连接
httpUrlConnection.connect();
//获取响应码
code = new Integer(httpUrlConnection.getResponseCode()).toString();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
httpUrlConnection.disconnect();
} catch (Exception e) {
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return code;
}
}