java之 24天 TCP,URL (二)

系统 1666 0
自定义简单 IE 1.采用 简单的GUI
    

import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
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.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class MyIEByGUI {

	private Frame f;
	private TextField tf;
	private Button btn;
	private TextArea ta;
	private Dialog d;
	private Label lab;
	private Button okbtn;
	
	MyIEByGUI(){
		init();
	}
	
	private 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,"提示信息-self",true);
		d.setBounds(400,200,250,150);
		d.setLayout(new FlowLayout());
		
		lab=new Label();
		okbtn=new Button("确定");
		
		d.add(lab);
		d.add(okbtn);
		
		
		f.add(tf);
		f.add(btn);
		f.add(ta);
		
		myEvent();
		f.setVisible(true);
		
	}
	
	
	private void myEvent() {
		
		//添加 回车 时候 自动转到
		tf.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					showDir();
				}
			}
		});
		//确定按钮事件
		okbtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				d.setVisible(false);
			}
		});
		
		d.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				d.setVisible(false);
			}
		
		});
		
		btn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				showDir();
			}
			
		});
		
		
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
			
		});
		
	}
	private void showDir() {
		try{
			String url=tf.getText();  //http://192.168.1.121:10000/myweb/demo.html
			
			int index=url.indexOf("//")+2;
			int index2=url.indexOf("/",index);
			
			String str=url.substring(index,index2);//192.168.1.121:10000
			String[] arr=str.split(":"); //192.168.1.121      10000
			String host=arr[0];
			int prot=Integer.parseInt(arr[1]);
			
			String path=url.substring(index2);  //myweb/demo.html
			
			Socket s=new Socket(host,prot);
			PrintWriter out=new PrintWriter(s.getOutputStream(),true);
			
			out.println("GET "+path+" HTTP/1.1");
			out.println("Accept: */*");
			out.println("Accept-Language: zh-CN");
			out.println("Host: "+str);
			//out.println("Connection: Keep-Alive");  请求完后 还保持连接,直到  请求超时
			out.println("Connection: closed");     //请求完后,连接就关闭
			out.println();  //结束头部请求
			
			BufferedReader bufr=new BufferedReader(new InputStreamReader(s.getInputStream()));
			
			String line=null;
			while((line=bufr.readLine())!=null){
				ta.append(line+"\r\n");
			}
			s.close();
			
			
			tf.setText("");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		new MyIEByGUI();
	}

}

  


自定义IE2,采用简单的GUI
    
import java.awt.Button;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.TextArea;
import java.awt.TextField;
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.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;

public class MyIEByGUI1 {

	private Frame f;
	private TextField tf;
	private Button btn;
	private TextArea ta;
	private Dialog d;
	private Label lab;
	private Button okbtn;
	
	MyIEByGUI1(){
		init();
	}
	
	private 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,"提示信息-self",true);
		d.setBounds(400,200,250,150);
		d.setLayout(new FlowLayout());
		
		lab=new Label();
		okbtn=new Button("确定");
		
		d.add(lab);
		d.add(okbtn);
		
		
		f.add(tf);
		f.add(btn);
		f.add(ta);
		
		myEvent();
		f.setVisible(true);
		
	}
	
	
	private void myEvent() {
		
		//添加 回车 时候 自动转到
		tf.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent e) {
				// TODO Auto-generated method stub
				if(e.getKeyCode()==KeyEvent.VK_ENTER){
					showDir();
				}
			}
		});
		//确定按钮事件
		okbtn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				d.setVisible(false);
			}
		});
		
		d.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				// TODO Auto-generated method stub
				d.setVisible(false);
			}
		
		});
		
		btn.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				showDir();
			}
			
		});
		
		
		f.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
			
		});
		
	}
	private void showDir() {
		try{
			String urlTest=tf.getText();  //http://192.168.1.121:10000/myweb/demo.html
			URL url=new URL(urlTest);
			URLConnection conn= url.openConnection();  // 在内部帮我们进行 socket 连接
			
			InputStream in=conn.getInputStream();
			byte[] buf=new byte[1024];
			int len=in.read(buf);
			
			ta.append(new String(buf,0,len));
			
			
			tf.setText("");
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	public static void main(String[] args) {
		new MyIEByGUI1();
	}

}

  


java之 24天 TCP,URL (二)

java之 24天 TCP,URL (二)


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论