HttpURLConnection 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
/**
* @param _url :访问网络的url地址
* @return boolean 是否连接成功
*/
public boolean postMonitorGet(String _url) {
HttpURLConnection httpConn = null;
BufferedInputStream bis = null;
try {
URL urlObj = new URL(_url);
httpConn = (HttpURLConnection) urlObj.openConnection();
httpConn.setRequestMethod("GET");
httpConn.setDoInput(true);
httpConn.setConnectTimeout(2000);
httpConn.connect();
if (httpConn.getResponseCode() == 200) {
bis = new BufferedInputStream(httpConn.getInputStream());
String s = new String(streamToByte(bis));
return true;
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
httpConn.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
} }

public static byte[] streamToByte(InputStream is) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int c = 0;
byte[] buffer = new byte[8 * 1024];
try {
while ((c = is.read(buffer)) != -1) {
baos.write(buffer, 0, c);
baos.flush();
}
return baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (baos != null) {
baos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

Peace.