网络编程--自定义浏览器

来源:互联网 发布:建筑设计平面图软件 编辑:程序博客网 时间:2024/06/08 16:07
/*演示客户端和服务端。客户端:浏览器服务端:自定义*/import java.net.*;import java.io.*;class ServerDemo{public static void main(String[] args) throws Exception{ServerSocket ss = new ServerSocket(1000);Socket s = ss.accept();System.out.println(s.getInetAddress().getHostAddress());PrintWriter pwOut = new PrintWriter(s.getOutputStream(),true);out.println("客户端你好");s.close();ss.close();}}/*命令行下:telnet 192.168.1.254 1000*//*演示客户端和服务端。客户端:浏览器服务端:Tomcat服务器在xampp htos文件夹下新建网页通过127.0.0.1:80+自己的网页即可打开*//*客户端:自定义服务端:Tomcat服务器*/import java.net.*;import java.io.*;class MyIE{public static void main(String[] args) throws Exception{Socket s = new Socket("192.168.1.254",80);PrintWriter pwOut = new PrintWriter(s.getOutputStream());pwOut.println("GET /myweb/demo.html HTTP/1.1");pwOut.println("Accept: */*");pwOut.println("Accept-Language: zh-cn");pwOut.println("GET /myweb/demo.html HTTP/1.1");pwOut.println("Host: 192.168.254:11000");pwOut.println("Connection: closed");pwOut.println();BufferedReader brIn = new BufferedReader(new InputStreamReader(s.getInputStream()));String line = null;while((line = brIn.readLine())!=null)System.out.println(line);s.close();}}/*自定义图形界面浏览器*/import java.awt.*;import java.awt.event.*;import java.io.*;class MyIE{private Frame f;private TextField tf;private Button btn;private TextArea ta;private Dialog d;private Label l;private Button okBtn;MyIE(){init();}public void init(){f = new Frame("my window");f.setBounds(300,100,600,500);f.setLayout(new FlowLayout());tf = new TextField(60);btn = new Button("转到");ta = new TextArea(25,70);//d = new Dialog(f,"提示信息",true);//d.setBounds(400,200,300,150);//d.setLayout(new FlowLayout());//l = new Label();//okBtn = new Button("确定")//d.add(lab);//d.add(okBtn);f.add(tf);f.add(btn);f.add(ta);myEvent();f.setVisible(true);}public void myEvent(){f.addWindowListener(new WindowListener(){public void windowClosing(WindowEvent e){System.exit(0);}});/*d.addWindowListener(new WindowListener(){public void windowClosing(WindowEvent e){d.setVisible(false);}});*/btn.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){try{showDir();}catch (Exception){}}});okBtn.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e){d.setVisible(false);tf.setText("");}});tf.addKeyListener(new KeyAdapter(){public void keyPressed(KeyEvent){try{if(e.getKeyCode()==KeyEvent.VK_ENTER)showDir();}catch (Exception e){}}});public void showDir()throws Exception{ta.setText("");String url = tf.getText();//http://192.168.1.254:80/myweb/demo.html;//截取int index1 = url.indexOf("//")+2;int index2 = url.indexOf("/",index1);//从“//”后开始截取,直到"/"String str = url.substring(index1,index2);//str = 192.168.1.254:80String[] arr = str.split(":");String host = arr[0];int port = Integer.parseInt(arr[1]);String path = url.substring(index2); //path = /myweb/demo.htmlSocket s = new Socket(host,port);PrintWriter pwOut = new PrintWriter(s.getOutputStream());pwOut.println("GET "+path" HTTP/1.1");pwOut.println("Accept: */*");pwOut.println("Accept-Language: zh-cn");pwOut.println("GET /myweb/demo.html HTTP/1.1");pwOut.println("Host: 192.168.254:11000");pwOut.println("Connection: closed");pwOut.println();BufferedReader brIn = new BufferedReader(new InputStreamReader(s.getInputStream()));String line = null;while((line = brIn.readLine())!=null)ta.append(line+"\r\n");s.close();}}public static void main(String[] args){new MyIE();}}


0 0
原创粉丝点击