package com.huizhi;
import java.net.*;
import java.io.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class SimpleBrowser extends JFrame implements ActionListener{
JToolBar toolBar;
JButton addButton;
JTextArea textArea;
JTextField addrFiled;
SimpleBrowser(){
addButton=new JButton("地址");
addrFiled=new JTextField(100);
textArea=new JTextArea(50,100);
textArea.setEnabled(false);
toolBar=new JToolBar();
toolBar.add(addButton);
toolBar.add(addrFiled);
add("North",toolBar);
add("Center",textArea);
addrFiled.addActionListener(this);
setTitle("浏览器");
setBounds(100,100,500,400);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
System.out.println("ok");
String addr=addrFiled.getText();
try{
StringTokenizer s=new StringTokenizer(addr,":/");
String protocol=s.nextToken();
String host=s.nextToken();
String port=s.nextToken();
String file=s.nextToken();
System.out.println("addr");
System.out.println(protocol+"-"+host+"-"+port+"-"+file);
Socket socket=new Socket(host,Integer.parseInt(port));
OutputStream os=socket.getOutputStream();
PrintWriter out=new PrintWriter(socket.getOutputStream(),true);
BufferedReader br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
out.println("GET /"+file+" HTTP/1.1");
char[] data=new char[1024];
while (true){
if(br.ready()){
int i=br.read(data);
break;
}
}
br.close();
textArea.setText(new String(data));
socket.close();
}
catch (Exception e1){
e1.printStackTrace();
}
}
public static void main(String[] args) {
new SimpleBrowser();
}
}