TE.java 2.07 KB
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();
	}
}

}