TE.java
2.07 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
package com.cnlive.socket;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.StatusLine;
import org.apache.commons.httpclient.methods.GetMethod;
public class TE {
public static void main(String[] args) {
new TE().doGet("http://bc.cnlive.com/live/epg/play/app?app_id=60_irg1rhs308&channelID=1471754157393");
}
private HttpClient getHttpClient() {
HttpClient client = new HttpClient();
client.getHttpConnectionManager().getParams().setConnectionTimeout(2200);
client.getHttpConnectionManager().getParams().setSoTimeout(2200);
return client;
}
public void doGet(String url) {
BufferedReader in = null;
HttpClient client = this.getHttpClient();
HttpMethod method = new GetMethod(url);
method.setFollowRedirects(false);
InputStream stream = null;
try {
int exeCode = client.executeMethod(method);
StatusLine status = method.getStatusLine();
int statusCode = status.getStatusCode();
StringBuffer buf = new StringBuffer();
if (statusCode == 301 || statusCode == 302) {
Header[] hs = method.getResponseHeaders("Location");
for (Header h : hs) {
System.out.println(h.getName() + "--" + h.getValue());
String playUrl = h.getValue();
}
} else {
stream = method.getResponseBodyAsStream();
in = new BufferedReader(new InputStreamReader(stream, "UTF-8"));
String line;
while (null != (line = in.readLine())) {
buf.append(line).append("\n");
}
System.out.println(buf.toString());
}
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
if (stream != null) {
try {
stream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
method.releaseConnection();
}
}
}