java

位置:IT落伍者 >> java >> 浏览文章

java读取网站内容的两种方法


发布日期:2024年08月25日
 
java读取网站内容的两种方法

HttpClient

利用apache的虚拟客户端包获取某个地址的内容 import javaioUnsupportedEncodingException;

import javautilHashSet;

import javautilIterator;

import javautilSet;

import javautilregexMatcher;

import javautilregexPattern;

import mons;

import mons;

import mons;

public class catchMain {

/** *//**

* @param args

*/

public static void main(String[] args) {

String url = ;

String keyword=;

String response=createhttpClient(urlkeyword);

}

public static String createhttpClient(String urlString param){

HttpClient client = new HttpClient();

String response=null;

String keyword=null;

PostMethod postMethod = new PostMethod(url);

try {

if(param!=null)

keyword = new String(paramgetBytes(gb)ISO);

} catch (UnsupportedEncodingException e) {

// TODO Autogenerated catch block

eprintStackTrace();

}

NameValuePair[] data = { new NameValuePair(keyword keyword) };

// 将表单的值放入postMethod中

postMethodsetRequestBody(data);

try {

int statusCode = clientexecuteMethod(postMethod);

response = new String(postMethodgetResponseBodyAsString()

getBytes(ISO) GBK);

} catch (Exception e) {

eprintStackTrace();

}

return response;

}

java自带的HttpURLConnection

public static String getPageContent(String strUrl String strPostRequest

int maxLength) {

//读取结果网页

StringBuffer buffer = new StringBuffer();

SystemsetProperty(clientdefaultConnectTimeout );

SystemsetProperty(clientdefaultReadTimeout );

try {

URL newUrl = new URL(strUrl);

HttpURLConnection hConnect = (HttpURLConnection) newUrl

openConnection();

//POST方式的额外数据

if (strPostRequestlength() > ) {

hConnectsetDoOutput(true);

OutputStreamWriter out = new OutputStreamWriter(hConnect

getOutputStream());

outwrite(strPostRequest);

outflush();

outclose();

}

//读取内容

BufferedReader rd = new BufferedReader(new InputStreamReader(

hConnectgetInputStream()));

int ch;

for (int length = ; (ch = rdread()) >

&& (maxLength <= || length < maxLength); length++)

bufferappend((char) ch);

rdclose();

hConnectdisconnect();

return buffertoString()trim();

} catch (Exception e) {

// return 错误:读取网页失败!;

return null;

}

}

               

上一篇:Java调用com组件操作word使用总结

下一篇:Spring AOP面向方面编程原理:AOP概念