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

📄 urlconnectiontest.java

📁 用java编写的URL连接测试程序
💻 JAVA
字号:
//Listing 21-2 -- URLConnectionTest.java 
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*; 
import java.net.*;
public class URLConnectionTest extends Applet implements ActionListener {
	private URL url_site;					// URL for connection
	private URLConnection uc_connection;	// URL Connection
	// Add buttons
	private Button bu_microsoft = new Button("Microsoft");
	private Button bu_sun = new Button("Sun");
	private Button bu_connect = new Button("Connect");
	// Add Labels for fields
	private Label lbl_URLString;
   	private Label lbl_DoInput;
	private Label lbl_DoOutput;
	private Label lbl_UseCaches;
	private Label lbl_ContentEncoding;
	private Label lbl_ContentLength;
	private Label lbl_ContentType;
	private Label lbl_Date;
	private Label lbl_Expiration;
	private Label lbl_LastModified;
	private Label lbl_IfModifiedSince;
	public void init() {		// init called when applet starts
	//Define Panels
		Panel northPanel = new Panel();
		Panel centerPanel = new Panel();
		Panel southPanel = new Panel();
		Panel fieldPanel = new Panel();
		Panel labelPanel = new Panel();
	// Set Panel and Applet layouts
		setLayout(new BorderLayout());
		northPanel.setLayout(new BorderLayout());
		centerPanel.setLayout(new BorderLayout());
		labelPanel.setLayout(new GridLayout(0,1));
		fieldPanel.setLayout(new GridLayout(0,1));
		southPanel.setLayout(new FlowLayout());
	//Add buttons to the appropriate Panels
		southPanel.add(bu_microsoft);
		southPanel.add(bu_sun);
		southPanel.add(bu_connect);
	//Add URL name to the north panel
		northPanel.add("West", new Label("URL:  ", Label.RIGHT));
		northPanel.add("Center", lbl_URLString = new Label());
	//Add labels to the labelPanel
		labelPanel.add(new Label("DoInput:  ", Label.RIGHT));
		labelPanel.add(new Label("DoOutput:  ", Label.RIGHT));
		labelPanel.add(new Label("UseCaches:  ", Label.RIGHT));
		labelPanel.add(new Label("ContentEncoding:  ", Label.RIGHT));
		labelPanel.add(new Label("ContentLength:  ", Label.RIGHT));
		labelPanel.add(new Label("ContentType:  ", Label.RIGHT));
		labelPanel.add(new Label("Date:  ", Label.RIGHT));
		labelPanel.add(new Label("Expiration:  ", Label.RIGHT));
		labelPanel.add(new Label("LastModified:  ", Label.RIGHT));
		labelPanel.add(new Label("IfModifiedSince:  ", Label.RIGHT));
	//Add label "fields" to the fieldPanel
		fieldPanel.add(lbl_DoInput = new Label());
		fieldPanel.add(lbl_DoOutput = new Label());
		fieldPanel.add(lbl_UseCaches = new Label());
		fieldPanel.add(lbl_ContentEncoding = new Label());
		fieldPanel.add(lbl_ContentLength = new Label());
		fieldPanel.add(lbl_ContentType = new Label());
		fieldPanel.add(lbl_Date = new Label());
		fieldPanel.add(lbl_Expiration = new Label());
		fieldPanel.add(lbl_LastModified = new Label());
		fieldPanel.add(lbl_IfModifiedSince = new Label());
	//Add ActionListeners for all buttons
		bu_connect.addActionListener(this);
		bu_microsoft.addActionListener(this);
		bu_sun.addActionListener(this);
	//Arrange Panels
		centerPanel.add("West", labelPanel);
		centerPanel.add("Center", fieldPanel);
		add("North", northPanel);
		add("South", southPanel);
		add("Center", centerPanel);
	}
	public void showWeb(String s) {
		clearFields();
		try {
			uc_connection = null;
			url_site = null;
			url_site = new URL(s);
			uc_connection = url_site.openConnection();
			uc_connection.connect();
			webInfo();
		}
		catch (MalformedURLException mue) {
			showStatus ("A MalformedURLException has occurred");
			url_site = null;
		}
		catch (IOException ioe) {
			showStatus ("A IOException has occurred");
			uc_connection = null;
			url_site = null;
		}
	}
	public void clearFields() {		//Clear text fields
		lbl_URLString.setText("");
   		lbl_DoInput.setText("");
		lbl_DoOutput.setText("");
		lbl_UseCaches.setText("");
		lbl_ContentEncoding.setText("");
		lbl_ContentLength.setText("");
		lbl_ContentType.setText("");
		lbl_Date.setText("");
		lbl_Expiration.setText("");
		lbl_LastModified.setText("");
		lbl_IfModifiedSince.setText("");
	}
	public void webInfo() {
		showStatus("Retrieving Web information.  Please wait...");
	// Available before connection
		lbl_URLString.setText(url_site.toString());
	    lbl_DoInput.setText(String.valueOf(uc_connection.getDoInput()));
	    lbl_DoOutput.setText(String.valueOf(uc_connection.getDoOutput()));
        lbl_UseCaches.setText(String.valueOf(uc_connection.getUseCaches()));
	// Available after connection
		lbl_ContentEncoding.setText(uc_connection.getContentEncoding());
		lbl_ContentLength.setText(String.valueOf(uc_connection.getContentLength()));
		lbl_ContentType.setText(uc_connection.getContentType());
		Date holdDate = new Date(uc_connection.getDate());
		lbl_Date.setText(holdDate.toString());
		holdDate = null;
		holdDate = new Date(uc_connection.getExpiration());
	    lbl_Expiration.setText(holdDate.toString());
		holdDate = null;
		holdDate = new Date(uc_connection.getLastModified());
        lbl_LastModified.setText(holdDate.toString());
		holdDate = null;
		holdDate = new Date(uc_connection.getIfModifiedSince());
	//	lbl_IfModifiedSince.setText(holdDate.toLocaleString());
        lbl_IfModifiedSince.setText(holdDate.toString());
		showStatus("Retrieval is finished");
	}
//The actionPerformed method is required 
//by the ActionListener interface
	public void actionPerformed(ActionEvent e) {
		// Get the text of the button
		String buttonText = e.getActionCommand();
		//Test for buttons
		if (buttonText.equals("Microsoft")) {
			showWeb("http://www.microsoft.com/visualj");
		}		
		else if (buttonText.equals("Sun")) {
			showWeb("http://www.sun.com/java");
		}
		else if (buttonText.equals("Connect")) {
			showStatus("Connecting to web site...Please wait.");
			getAppletContext().showDocument(url_site);	//Connect to the URL
			showStatus("");
		}		
	}
}

⌨️ 快捷键说明

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