day24/MyIE.java

来源:互联网 发布:python while循环游戏 编辑:程序博客网 时间:2024/05/17 07:52
/*客户端:自定义服务端:tomcat服务器需求:自定义一个客户端,向tomcat服务器发请求,要求浏览demo.html页面。自定义客户端用到了图形化界面。*/import java.awt.*;import java.awt.event.*;import java.io.*;import java.net.*;class MyIE {private Frame f;private Button but;private TextField tf;private TextArea ta;MyIE(){init();}public void init(){f=new Frame("IE浏览器");f.setBounds(300,150,600,500);//坐标x y x yf.setLayout(new FlowLayout());tf=new TextField(50);f.add(tf);but=new Button("转到");f.add(but);ta=new TextArea(25,60);//25行,60列f.add(ta);myEvent();f.setVisible(true);}private void myEvent(){tf.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent e){try{if(e.getKeyCode()==KeyEvent.VK_ENTER)showDir();}catch (Exception ex){}}});but.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){try{showDir();}catch (Exception ex){}}});f.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){System.exit(0);}});}public void showDir()throws Exception{ta.setText("");String url = tf.getText();//http://192.168.1.101:8080/myweb/demo.htmlint index1 = url.indexOf("//")+2;int index2 = url.indexOf("/",index1);String str = url.substring(index1,index2);String path=url.substring(index2);String[] arr = str.split(":");String ip = arr[0];int port = Integer.parseInt(arr[1]);Socket s = new Socket(ip,port);PrintWriter out = new PrintWriter(s.getOutputStream(),true);//out.println("GET /myweb/demo.html HTTP/1.1");//向tomcat端请求浏览demo.html网页out.println("GET "+path+" HTTP/1.1");//向tomcat端请求浏览demo.html网页out.println("Accept:*/*");out.println("Accept-Language: zh-cn");out.println("Host: 192.168.1.101:8080");out.println("Connection: closed");out.println();out.println();InputStream in = s.getInputStream();byte[] buf = new byte[1024];int len=0;while((len=in.read(buf))!=-1){ta.append(new String(buf,0,len));}s.close();}public static void main(String[] args) throws Exception{new MyIE();}}

0 0
原创粉丝点击