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

《Java读取网站内容的两种方法》由会员分享,可在线阅读,更多相关《Java读取网站内容的两种方法(4页珍藏版)》请在人人文库网上搜索。

1、Java读取网站内容的两种方法一、HttpClient利用apache的虚拟客户端包获取某个地址的内容 1import java.io.UnsupportedEncodingException;2import java.util.HashSet;3import java.util.Iterator;4import java.util.Set;5import java.util.regex.Matcher;6import java.util.regex.Pattern;78import org.apache.commons.httpclient.HttpClient;9import org.ap。

2、ache.commons.httpclient.NameValuePair;10import org.apache.commons.httpclient.methods.PostMethod;1112public class catchMain 1314 /* */*15 * param args16 */17 public static void main(String args) 181920 String url = “http:/search.foodqs.com/companysearch.asp”;21 String keyword=“食”;22 String response=c。

3、reatehttpClient(url,keyword);23 2425public static String createhttpClient(String url,String param)26 HttpClient client = new HttpClient();27 String response=null;28 String keyword=null;29 PostMethod postMethod = new PostMethod(url);30 try 31 if(param!=null)32 keyword = new String(param.getBytes(“gb2。

4、312”),“ISO-8859-1”);33 catch (UnsupportedEncodingException e1) 34 / TODO Auto-generated catch block35 e1.printStackTrace();36 3738 NameValuePair data = new NameValuePair(“keyword”, keyword) ;39 / 将表单的值放入postMethod中40 postMethod.setRequestBody(data);4142 try 43 int statusCode = client.executeMethod(p。

5、ostMethod);44 response = new String(postMethod.getResponseBodyAsString()45 .getBytes(“ISO-8859-1”), “GBK”);46 catch (Exception e) 4748 e.printStackTrace();49 50 return response;5152 53二、java自带的HttpURLConnection1public static String getPageContent(String strUrl, String strPostRequest,2 int maxLength)。

6、 3 /读取结果网页4 StringBuffer buffer = new StringBuffer();5 System.setProperty(“sun.net.client.defaultConnectTimeout”, “5000”);6 System.setProperty(“sun.net.client.defaultReadTimeout”, “5000”);7 try 8 URL newUrl = new URL(strUrl);9 HttpURLConnection hConnect = (HttpURLConnection) newUrl10 .openConnection。

7、();11 /POST方式的额外数据12 if (strPostRequest.length() 0) 13 hConnect.setDoOutput(true);14 OutputStreamWriter out = new OutputStreamWriter(hConnect15 .getOutputStream();16 out.write(strPostRequest);17 out.flush();18 out.close();19 20 /读取内容21 BufferedReader rd = new BufferedReader(new InputStreamReader(22 hConnect.getInputStream();23 int ch;24 for (int length = 0; (ch = rd.read() -125 & (maxLength = 0 | length maxLength); length+)26 buffer.append(char) ch);27 rd.close();28 hConnect.disconnect();29 return buffer.toString().trim();30 catch (Exception e) 31 / return “错误:读取网页失败!”;32 return null;33 34。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值