当前位置: 首页 > 工具软件 > io.spider > 使用案例 >

java spider_Spider.java

田玉韵
2023-12-01

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;

}

}

 类似资料:

相关阅读

相关文章

相关问答