欢迎来到天天文库
浏览记录
ID:11612511
大小:32.00 KB
页数:4页
时间:2018-07-13
《java读取网站内容的两种方法》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Java读取网站内容的两种方法一、HttpClient利用apache的虚拟客户端包获取某个地址的内容1importjava.io.UnsupportedEncodingException;2importjava.util.HashSet;3importjava.util.Iterator;4importjava.util.Set;5importjava.util.regex.Matcher;6importjava.util.regex.Pattern;78importorg.apache.commons.httpclient.
2、HttpClient;9importorg.apache.commons.httpclient.NameValuePair;10importorg.apache.commons.httpclient.methods.PostMethod;1112publicclasscatchMain{1314/***//**15*@paramargs16*/17publicstaticvoidmain(String[]args){181920Stringurl=“http://search.foodqs.com/companysearch.a
3、sp”;21Stringkeyword=“食”;22Stringresponse=createhttpClient(url,keyword);23}2425publicstaticStringcreatehttpClient(Stringurl,Stringparam){26HttpClientclient=newHttpClient();27Stringresponse=null;28Stringkeyword=null;29PostMethodpostMethod=newPostMethod(url);30try{31if(
4、param!=null)32keyword=newString(param.getBytes(“gb2312”),“ISO-8859-1”);33}catch(UnsupportedEncodingExceptione1){34//TODOAuto-generatedcatchblock35e1.printStackTrace();36}3738NameValuePair[]data={newNameValuePair(“keyword”,keyword)};39//将表单的值放入postMethod中40postMethod.
5、setRequestBody(data);4142try{43intstatusCode=client.executeMethod(postMethod);44response=newString(postMethod.getResponseBodyAsString()45.getBytes(“ISO-8859-1”),“GBK”);46}catch(Exceptione){4748e.printStackTrace();49}50returnresponse;5152}53二、java自带的HttpURLConnection1
6、publicstaticStringgetPageContent(StringstrUrl,StringstrPostRequest,2intmaxLength){3//读取结果网页4StringBufferbuffer=newStringBuffer();5System.setProperty(“sun.net.client.defaultConnectTimeout”,“5000”);6System.setProperty(“sun.net.client.defaultReadTimeout”,“5000”);7try{8U
7、RLnewUrl=newURL(strUrl);9HttpURLConnectionhConnect=(HttpURLConnection)newUrl10.openConnection();11//POST方式的额外数据12if(strPostRequest.length()》0){13hConnect.setDoOutput(true);14OutputStreamWriterout=newOutputStreamWriter(hConnect15.getOutputStream());16out.write(strPost
8、Request);17out.flush();18out.close();19}20//读取内容21BufferedReaderrd=newBufferedReader(newInputStreamReader(22hConnect.getInputStream
此文档下载收益归作者所有