-------------android培训、java培训、期待与您交流----------------------
package com.itheima;
import java.awt.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;
public class MyIEByGUI {
private Frame fm;
private TextField tf;
private TextArea ta;
private Button bt;
MyIEByGUI(){
try {
init();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void init() throws Exception{
fm = new Frame("my Window");
fm.setBounds(100, 100, 600, 400);
fm.setLayout(new FlowLayout());
tf = new TextField(60);
bt = new Button(" 转到");
ta = new TextArea(25, 70);
fm.add(tf);
fm.add(bt);
fm.add(ta);
myEvent();
fm.setVisible(true);
}
public void myEvent() throws Exception{//创建各个组件的监听事件
tf.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER)
try {
showDir();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
bt.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
try {
showDir();
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
fm.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e){
System.exit(0);;
}
});
}
public void showDir() throws Exception{//显示服务器发送回来的信息到文本域框中
ta.setText("");
String url = tf.getText();
int index1 = url.indexOf("//")+2;
int index2 = url.indexOf("/", index1);
String str = url.substring(index1, index2);
String[] arr = str.split(":");
String host = arr[0];
int port = Integer.parseInt(arr[1]);
String path = url.substring(index2);
//ta.setText(str+"---"+path);
Socket s = new Socket(host,port);//创建客户端Socket对象
PrintWriter out = new PrintWriter(s.getOutputStream(),true);//创建字符流PrintWriter对象关联字节输出流
out.println("GET /myweb/demo.html HTTP/1.1");//发送信息(get方式访问服务器)
out.println("Accept:*/*");//接收所有类型的文件
out.println("Accept-Language:zh-cn");//接收简体中文
out.println("Host:192.168.1.254:8080");//发送到的主机地址和端口
out.println("Connection:Keep-Alive");//连接状态:保持存活
out.println("");
out.println("");//空行用于和主体消息隔开
BufferedReader buf = new BufferedReader(new InputStreamReader(s.getInputStream
()));///创建缓冲字符流关联字节输入流
String line = null;//定义字符串变量用于接收一行输入字符流
while((line=buf.readLine())!=null){//读取到的字符输入流不为空则打印信息
ta.append(line+"\r\t");
}
s.close();
}
public static void main(String[] args){
new MyIEByGUI();
}
}