import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.net.Socket;
import java.io.*;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class IE {
private JFrame frame;
private JTextField jtf;
private JTextArea jta;
private JButton button;
private Dialog dl;
private JLabel label;
private JButton okButton;
public IE(){
init();
initDialog();
}
private void init(){
frame=new JFrame("IE");
frame.setSize(500,400);
jtf=new JTextField(30);
jta=new JTextArea(50,40);
button=new JButton("转到");
frame.setLayout(new FlowLayout());
frame.add(jtf);
frame.add(button);
frame.add(jta);
addEventOnFrame();
frame.setVisible(true);
}
public void initDialog(){
dl=new Dialog(frame,"错误提示",true);
dl.setBounds(80,80,200,200);
label=new JLabel();
dl.setLayout(new FlowLayout());
dl.add(label);
okButton=new JButton("确定");
dl.add(okButton);
addEventOnDialog();
}
private void addEventOnFrame(){
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
listDir();
}
});
jta.addKeyListener(new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER)
listDir();
}
});
}
private void addEventOnDialog(){
okButton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
dl.setVisible(false);
}
});
dl.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
okButton.addKeyListener(new KeyAdapter(){
@Override
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER)
dl.setVisible(false);
}
});
}
private void listDir(){
jta.setText("");
String url=jtf.getText();
int index1=url.indexOf("//")+2;
int index2=url.indexOf("/",index1);
//截取IP地址和端口号
String str=url.substring(index1,index2);
String[]arr=str.split(":");
if(arr.length!=2){
label.setText(url+"输入有误");
dl.setVisible(true);
}
String host=arr[0];
int port=Integer.parseInt(arr[1]);
jta.setText(host+"..."+port);
Socket s=null;
PrintStream ps=null;
try{
s=new Socket(host,port);
ps=new PrintStream(s.getOutputStream());
ps.println("GET/demo.html HTTP/1.1");
ps.println("Accept:*/*");
ps.println("Accept-Language:zh-CN");
ps.println("Connection:Keep-alive");
ps.println();
ps.println();
BufferedReader br=
new BufferedReader(
new InputStreamReader(s.getInputStream()));
String line;
while((line=br.readLine())!=null){
jta.append(line+"\r\n");
}
ps.close();
s.close();
}catch(IOException e){
e.printStackTrace();
}
}
public static void main(String []args){
new IE();
}
}
输入http://127.0.0.1:8080/demo.html
为什么老抛
java.net.SocketException: Connection reset
at java.net.SocketInputStream.read(SocketInputStream.java:168)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
at java.io.InputStreamReader.read(InputStreamReader.java:167)
at java.io.BufferedReader.fill(BufferedReader.java:136)
at java.io.BufferedReader.readLine(BufferedReader.java:299)
at java.io.BufferedReader.readLine(BufferedReader.java:362)
at IE.listDir(IE.java:123)
at IE.access$0(IE.java:93)
at IE$2.actionPerformed(IE.java:62)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6216)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
at java.awt.Component.processEvent(Component.java:5981)
at java.awt.Container.processEvent(Container.java:2041)