package com.test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Spider {
public static void main(String[] args) {
String url="http://www.cnblogs.com/xingyun/archive/2012/02/05/2339237.html";
System.out.println(GetResponseDataByID(url, null));
}
public static String GetResponseDataByID(String url, String postData) {
String content = null;
try {
URL dataUrl = new URL(url);
HttpURLConnection con = (HttpURLConnection) dataUrl
.openConnection();
// System.out.println(con.getResponseCode());
// System.out.println(con.getContentLength());
// con.setRequestMethod("POST");
// con.setRequestProperty("Proxy-Connection", "Keep-Alive");
// con.setDoOutput(true);
// con.setDoInput(true);
// OutputStream os = con.getOutputStream();
// DataOutputStream dos = new DataOutputStream(os);
// dos.write(postData.getBytes());
// dos.flush();
// dos.close();
//byte d[] = new byte[4];
String line;
InputStream is = con.getInputStream();
StringBuffer stringBuffer = new StringBuffer();
Reader reader = new InputStreamReader(is, "utf8");
// 增加缓冲功能
BufferedReader bufferedReader = new BufferedReader(reader);
while ((line = bufferedReader.readLine()) != null) {
stringBuffer.append(line + "\n");
}
if (bufferedReader != null) {
bufferedReader.close();
}
content = stringBuffer.toString();
} catch (Exception ex) {
ex.printStackTrace();
}
return content;
}
}