`

Java读URL, 如Q日志

 
阅读更多

①代码:http://feeds.qzone.qq.com/cgi-bin/cgi_rss_out?uin=QQ号码

主要难点在于URL获得, 其他简单:

 

package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class QQBlog {

	public static void main(String[] args) throws Exception {
		getBlog();
	}
	public static void getURL()  throws Exception{
		
		String urlString= "";
		URL url = new URL(urlString);

		// http://h5.qzone.qq.com/ugc/share?subtype=0&sk=&blog_photo=0&appid=2&ciphertext=696787C2AB42A61C2E27685CEE36AF9796854678E0B93AEFF61B57E96CB504643D243384B5184728762F01C1DCEB0F0C&g_f=
		// http://b.qzone.qq.com/cgi-bin/blognew/blog_output_data?bdm=b.qzone.qq.com&blogid=&imgdm=ctc.qzs.qq.com&mode=2&numperpage=15&property=GoRE&uin=QQ号码
		// http://b.qzone.qq.com/cgi-bin/blognew/blog_get_titlelist?uin=635998173&vuin=0&property=GoRE&category=&numperpage=100&pos=0&direct=1
		// http://user.qzone.qq.com/635998173/blog/1441881523
		// http://feeds.qzone.qq.com/cgi-bin/cgi_rss_out?uin=635998173
		// http://url.cn/e9gVNW
		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		connection.connect();

		InputStream urlStream = connection.getInputStream();
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				urlStream));
		String tmp = "";
		StringBuffer sb = new StringBuffer();
		while ((tmp = reader.readLine()) != null) {
			sb.append(tmp);
		}
		System.out.println(sb.toString());
	}

	public static void getBlog() {
		HttpClient client = new HttpClient();
		HttpMethod method = null;
		String charset = "utf-8";
		String urlString= "";
		method = new GetMethod(urlString);
		method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
				charset);
		int status = 0;
		try {
			status = client.executeMethod(method);
		} catch (HttpException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (status != HttpStatus.SC_OK) {
			return;
		}
		BufferedWriter bw = null;
		try {
			String toWrite = method.getResponseBodyAsString();
			if (toWrite != null) {
				File file = new File("d://abc.txt");
				if (!file.exists()) {
					file.createNewFile();
				}
				bw = new BufferedWriter(new FileWriter(file));
				bw.write(toWrite);
				System.out.println(toWrite.toString());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			method.releaseConnection();
			if (bw != null) {
				try {
					bw.flush();
					bw.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics