⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 browser.java

📁 用httpclient-4.0-alpha2 打造基于http协议的网站分析器
💻 JAVA
字号:
package com.ue.browser;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

import org.apache.http.Header;
import org.apache.http.HttpStatus;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.ClientContext;
import org.apache.http.impl.client.DefaultHttpClient;

import com.ue.browser.core.Element;
import com.ue.browser.exception.BrowseException;
import com.ue.browser.util.StringUtil;

public class Browser extends Element {

	private String url;
	private String jumpurl;
	private HttpClient client;
	private Document document;
	private Iframe iframe = new Iframe();
	public String homeurl;
	
	public static String BLANK_URL = "about:blank";
	public static String DEFAULT_BODY = "<html><body>Hello, world! <br>hoyzhang@163.com</body></html>";
	
		
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getHomeurl() {
		return homeurl;
	}

	public void setHomeurl(String homeurl) {
		this.homeurl = homeurl;
	}

	public HttpClient getClient() {
		return client;
	}

	public void setClient(HttpClient client) {
		this.client = client;
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}
	
	public Iframe getIframe() {
		return iframe;
	}

	public void setIframe(Iframe iframe) {
		this.iframe = iframe;
	}

	public Browser(){
		this(null);
	}
	
	public Browser(Browser browser) {
		super(null);
		this.document = new Document(this);
		this.client = new DefaultHttpClient();
		try{
			this.reset();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public String getLocation() {
		return this.url;
	}
	
	public void reset() throws BrowseException {
		this.document.setBody(DEFAULT_BODY);
		this.url = BLANK_URL;
		
		//clear cookies
		CookieStore cookieStore = (CookieStore)this.client.getDefaultContext().getAttribute(ClientContext.COOKIE_STORE);
		cookieStore.clear();
	}
	
	public void refresh() throws BrowseException {
		this.navigate(this.url);
	}
	
	//将浏览器停止在当前访问状态
	public void stop() throws BrowseException {
		this.document.setBody(this.document.getBody());
		this.url = this.getLocation();
	}
	
    //让浏览器访问主页URL
	public void home() throws BrowseException {
		this.navigate(this.homeurl);
	}
	
    //判断是否有跳转并返回跳转URL
	public String getJumpUrl(int statusCode,Header header) throws BrowseException {
		String newuri = ""; 
		if ((statusCode == HttpStatus.SC_MOVED_TEMPORARILY) ||
                (statusCode == HttpStatus.SC_MOVED_PERMANENTLY) ||
                (statusCode == HttpStatus.SC_SEE_OTHER) ||
                (statusCode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
       	 if (header != null) {
                newuri = header.getValue();
                if ((newuri == null) || (newuri.equals("")))   newuri = "/";
       	        }
       	 }
       return newuri;
	}
	
	public void navigate(String url) throws BrowseException {
		try{
			this.url = url;
			if(url.equals(BLANK_URL)){
				return;
			}
			
			HttpGet httpget = new HttpGet(url);
	        HttpResponse response = this.client.execute(httpget);
	        Header[] headers = response.getAllHeaders();
	        int statusCode = response.getStatusLine().getStatusCode();
	        //System.err.println("statusCode:="+statusCode);
	        
	        if(headers!= null ){
	        	for(int i=0;i<headers.length;i++){
	        		this.jumpurl = this.getJumpUrl(statusCode, headers[i]);
	        	//System.err.println(this.getJumpUrl(statusCode, headers[i]));
	        	}
	        }
	        
	        ByteArrayOutputStream  baos = new ByteArrayOutputStream();
	        HttpEntity entity = response.getEntity();
	        StringBuffer body = new StringBuffer();
	        String line = "";
	        if (entity != null) {
	        	InputStream is = entity.getContent();
	        	byte b=0;   
	            try {
	              while((b=(byte)is.read())!=-1){   
	                  baos.write(b);   
	              }} catch (IOException ex) {
		                throw ex;
		            } catch (RuntimeException ex) {
		                httpget.abort();
		                throw ex;
		            } 
	              
	        }
	                       
	        /*
	         if (entity != null) {
	            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
	            try {
	            	while((line=reader.readLine())!=null){
		            	body.append(line+"\n");
	            	}
	            } catch (IOException ex) {
	                throw ex;
	            } catch (RuntimeException ex) {
	                httpget.abort();
	                throw ex;
	            } finally {
	                reader.close();
	            }
	        }
	        */
	        
	        //System.out.println(baos.toString());
	        String newbody = StringUtil.SpecialString(baos.toString());
	        this.document.setBody(newbody);
	        this.document.setUrl(url);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public String JsNavigate(String jsurl) throws BrowseException {
		String js = "";
		try{
			if(jsurl.equals(BLANK_URL)){
				return js;
			}
			
			HttpGet httpget = new HttpGet(jsurl);
	        HttpResponse response = this.client.execute(httpget);

	        HttpEntity entity = response.getEntity();
	        StringBuffer body = new StringBuffer();
	        String line = "";
	        
	        if (entity != null) {
	            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
	            try {
	            	while((line=reader.readLine())!=null){
		            	body.append(line+"\n");
	            	}
	            } catch (IOException ex) {
	                throw ex;
	            } catch (RuntimeException ex) {
	                httpget.abort();
	                throw ex;
	            } finally {
	                reader.close();
	            }
	        }
	        //System.out.println(body.toString());
	        js = body.toString();
		}catch(Exception e){
			e.printStackTrace();
		}
		return js;
	}
	
	public String FrameNavigate(String furl) throws BrowseException {
		String fs = "";
		try{
			this.url = furl;
			if(furl.equals(BLANK_URL)){
				return fs;
			}
			
			HttpGet httpget = new HttpGet(furl);
	        HttpResponse response = this.client.execute(httpget);

	        HttpEntity entity = response.getEntity();
	        StringBuffer body = new StringBuffer();
	        String line = "";
	        
	        if (entity != null) {
	            BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
	            try {
	            	while((line=reader.readLine())!=null){
		            	body.append(line+"\n");
	            	}
	            } catch (IOException ex) {
	                throw ex;
	            } catch (RuntimeException ex) {
	                httpget.abort();
	                throw ex;
	            } finally {
	                reader.close();
	            }
	        }
	        //System.out.println(body.toString());
	        fs = body.toString();
	        //this.iframe.setSrc(furl);
		}catch(Exception e){
			e.printStackTrace();
		}
		return fs;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -