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

📄 urlcheck.java

📁 check the url link whether it is good or bad link
💻 JAVA
字号:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.StringTokenizer;

/**
 * 
 * @author Inzaghi
 *
 * This program is to find out HTTP status code of all links in input URL. 
 */

public class URLCheck{
	private URL url;
	
	public static enum HTTPStatusCodeEnum {
		Continue (100),
		Switching_Protocols (101),
		Processing (102),
	    OK (200),
	    Created(201),
	    Accepted(202),
	    Non_Authoritative_Information(203),
	    No_Content (204),
	    Reset_Content(205),
	    Partial_Content (206),
	    Multi_Status (207),
	    Multiple_Choices (300),
	    Moved_Permanently (301),
	    Found (302),
	    See_Other (303),
	    Not_Modified (304),
	    Use_Proxy (305),
	    Unused_306 (306),
	    Temporary_Redirect (307),
	    Bad_Request(400),
	    Authorization_Required  (401),
	    Payment_Required  (402),
	    Forbidden  (403),
	    Not_Found  (404),
	    Method_Not_Allowed  (405),
	    Not_Acceptable  (406),
	    Proxy_Authentication_Required (407),
	    Request_Time_out  (408),
	    Conflict (409),
	    Gone (410),
	    Length_Required  (411),
	    Precondition_Failed  (412),
	    Request_Entity_Too_Large  (413),
	    Request_URI_Too_Large  (414),
	    Unsupported_Media_Type  (415),
	    Requested_Range_Not_Satisfiable  (416),
	    Expectation_Failed  (417),
	    Unused_418 (418),
	    Unused_419  (419),
	    Unused_420  (420),
	    Unused_421  (421),
	    Unprocessable_Entity (422),
	    Locked  (423),
	    Failed_Dependency (424),
	    No_code  (425),
	    Upgrade_Required (426),
	    Internal_Server_Error (500),
	    Method_Not_Implemented (501),
	    Bad_Gateway (502),
	    Service_Temporarily_Unavailable (503), 
	    Gateway_Time_out (504),
	    HTTP_Version_Not_Supported (505), 
	    Variant_Also_Negotiates (506),
	    Insufficient_Storage (507),
	    Unused_508 (508),
	    Unused_509 (509),
	    Not_Extended (510),
		Other (-1);

		HTTPStatusCodeEnum(int value) {
			this.value = (short) value;
		}
		
		private short value;
		
		public short value() {
			return value;
		}

		public static HTTPStatusCodeEnum getEnum(String name) {
			for (HTTPStatusCodeEnum e : HTTPStatusCodeEnum.values()) {
				if (e.name().equalsIgnoreCase(name)) {
					return e;
				}
			}
			return null;
		}	
		
		public static String getString(int value) {
			for (HTTPStatusCodeEnum e : HTTPStatusCodeEnum.values()) {
				if (e.value == value) {
					return e.name();
				}
			}
			return null;
		}
		
		
	}
	
	//constructor
	public URLCheck(String requestedURL)
	{
		try {
			url = new URL(requestedURL);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//to get the html source code of the requested url
		String sHTML = readContents(requestedURL);
		
		//if no source code is found, exit program.
		if(sHTML.length() == 0)
		{
			System.exit(0);
		}
		
		//System.out.println(sHTML);
		
		//to get the list of links from the html source code
		ArrayList<String> array = getLinksFromHTML(sHTML); 
		
		//throwing each link to the thread to handle retrieving response code
		for(String urlStr : array)
		{
			Thread t = new Thread(new handleRetrieveHttpStatusCode(urlStr));
			t.start();
		}
	}
	
	//synchronized method to print out the output
	static public synchronized void printOutput(String url, int result)
	{

		
		System.out.println(url + " : "+ HTTPStatusCodeEnum.getString(result));
	}

	//to start url connection and retrieve the html source code
	private String readContents(String urlString) 
	{
		URLConnection connection;
		String sHTML = "";
	
		try 
		{
			connection = url.openConnection();
			//connected
		} 
		catch (MalformedURLException e)
		{
			e.printStackTrace();
			return null;
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
			return null;
		}
	
		//to read the contents
		BufferedReader in = null;
		
		try
		{
			in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

			String inputLine;

			while ((inputLine = in.readLine()) != null) 
			{
				sHTML += (inputLine);
			}
			
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				in.close();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
		}
		
		return sHTML;
	}
	
	//to get all links and place into arraylist from the html source code
	private ArrayList<String> getLinksFromHTML(String sHtml)
	{
		ArrayList<String> array = new ArrayList<String>();
		sHtml = sHtml.toLowerCase();
		
		int index = 0;
		while ((index = sHtml.indexOf("<a", index)) != -1)
		{
		    if ((index = sHtml.indexOf("href", index)) == -1) 
			break;
		    if ((index = sHtml.indexOf("=", index)) == -1) 
			break;

		    index++;
		    String remaining = sHtml.substring(index);

		    StringTokenizer st 
		      = new StringTokenizer(remaining, "\t\n\r\">#");
		    String strLink = st.nextToken();
		    
		    try {
		    	URL urlLink = new URL(url, strLink);
				strLink = urlLink.toString();
			    } catch (MalformedURLException e) {
			    	System.out.println("ERROR: bad URL " + strLink);
				continue;
			}
			    
			array.add(strLink);
		}

		return array;
	}

	public static void main(String[] args) 
	{
		if (args.length != 1) 
		{
			System.err.println("No Url is entered.");
			System.exit(0);
		}
		
		new URLCheck(args[0]);
	}
}

//Runnable class to handle thread of retrieving response code
class handleRetrieveHttpStatusCode implements Runnable
{
	private String urlString;
	
	public handleRetrieveHttpStatusCode(String urlString)
	{
		this.urlString = urlString;
	}
	
	private String getResponseCode() 
	{
		try 
		{
			URL url = new URL(urlString);
			
			//if the response is 302, it will automatically retrieved the redirected url.
			HttpURLConnection.setFollowRedirects(true);
			
			HttpURLConnection httpConnection = (HttpURLConnection)url.openConnection();
			
			URLCheck.printOutput(urlString,httpConnection.getResponseCode());
		} 
		catch (MalformedURLException e)
		{
			e.printStackTrace();
			return null;
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
			return null;
		}
		
		return null;
	}
	
	public void run()
	{
		getResponseCode() ;
	}
}

⌨️ 快捷键说明

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