這篇文章將為大家詳細(xì)講解有關(guān)如何解決java網(wǎng)絡(luò)爬蟲連接超時(shí)的問題,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
成都創(chuàng)新互聯(lián)主要業(yè)務(wù)有網(wǎng)站營(yíng)銷策劃、成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站、成都外貿(mào)網(wǎng)站建設(shè)公司、微信公眾號(hào)開發(fā)、微信小程序定制開發(fā)、HTML5建站、程序開發(fā)等業(yè)務(wù)。一次合作終身朋友,是我們奉行的宗旨;我們不僅僅把客戶當(dāng)客戶,還把客戶視為我們的合作伙伴,在開展業(yè)務(wù)的過程中,公司還積累了豐富的行業(yè)經(jīng)驗(yàn)、成都全網(wǎng)營(yíng)銷資源和合作伙伴關(guān)系資源,并逐漸建立起規(guī)范的客戶服務(wù)和保障體系。
具體如下。
在網(wǎng)絡(luò)爬蟲中,經(jīng)常會(huì)遇到如下報(bào)錯(cuò)。即連接超時(shí)。針對(duì)此問題,一般解決思路為:將連接時(shí)間、請(qǐng)求時(shí)間設(shè)置長(zhǎng)一下。如果出現(xiàn)連接超時(shí)的情況,則在重新請(qǐng)求【設(shè)置重新請(qǐng)求次數(shù)】。
Exception in thread "main" java.net.ConnectException: Connection timed out: connect
下面的代碼便是使用httpclient解決連接超時(shí)的樣例程序。直接上程序。
package daili; import java.io.IOException; import java.net.URI; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.params.CookiePolicy; import org.apache.http.client.protocol.ClientContext; import org.apache.http.impl.client.BasicCookieStore; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.client.DefaultHttpRequestRetryHandler; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.cookie.BasicClientCookie2; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.protocol.BasicHttpContext; import org.apache.http.protocol.ExecutionContext; import org.apache.http.protocol.HttpContext; import org.apache.http.util.EntityUtils; /* * author:合肥工業(yè)大學(xué) 管院學(xué)院 錢洋 *1563178220@qq.com */ public class Test1 { public static void main(String[] args) throws ClientProtocolException, IOException, InterruptedException { getRawHTML("http://club.autohome.com.cn/bbs/forum-c-2098-1.html#pvareaid=103447"); } public static String getRawHTML ( String url ) throws ClientProtocolException, IOException, InterruptedException{ //初始化 DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY); //設(shè)置參數(shù) HttpParams params = httpclient.getParams(); //連接時(shí)間 HttpConnectionParams.setConnectionTimeout(params, 6000); HttpConnectionParams.setSoTimeout(params, 6000*20); //超時(shí)重新請(qǐng)求次數(shù) DefaultHttpRequestRetryHandler dhr = new DefaultHttpRequestRetryHandler(5,true); HttpContext localContext = new BasicHttpContext(); HttpRequest request2 = (HttpRequest) localContext.getAttribute( ExecutionContext.HTTP_REQUEST); httpclient.setHttpRequestRetryHandler(dhr); BasicCookieStore cookieStore = new BasicCookieStore(); BasicClientCookie2 cookie = new BasicClientCookie2("Content-Type","text/html;charset=UTF-8"); BasicClientCookie2 cookie1 = new BasicClientCookie2("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36"); cookieStore.addCookie(cookie); cookieStore.addCookie(cookie1); localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet request = new HttpGet(); request.setURI(URI.create(url)); HttpResponse response = null; String rawHTML = ""; response = httpclient.execute(request,localContext); int StatusCode = response.getStatusLine().getStatusCode(); //獲取響應(yīng)狀態(tài)碼 System.out.println(StatusCode); if(StatusCode == 200){ //狀態(tài)碼200表示響應(yīng)成功 //獲取實(shí)體內(nèi)容 rawHTML = EntityUtils.toString (response.getEntity()); System.out.println(rawHTML); //輸出實(shí)體內(nèi)容 EntityUtils.consume(response.getEntity()); //消耗實(shí)體 } else { //關(guān)閉HttpEntity的流實(shí)體 EntityUtils.consume(response.getEntity()); //消耗實(shí)體 Thread.sleep(20*60*1000); //如果報(bào)錯(cuò)先休息30分鐘 } httpclient.close(); System.out.println(rawHTML); return rawHTML; } }
結(jié)果:
關(guān)于“如何解決java網(wǎng)絡(luò)爬蟲連接超時(shí)的問題”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
網(wǎng)頁(yè)名稱:如何解決java網(wǎng)絡(luò)爬蟲連接超時(shí)的問題
當(dāng)前URL:http://muchs.cn/article40/igshho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、域名注冊(cè)、品牌網(wǎng)站建設(shè)、外貿(mào)建站、做網(wǎng)站、網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)