/*
打开 Tomcat 服务器
输入:http://127.0.0.1:8080/myweb/demo.html
*/
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
class MyIEByGUI {
private Frame f;
private Button but;
private TextField tf;
private TextArea ta;
MyIEByGUI() {
init();
}
public void init() {
f = new Frame("my window");
f.setBounds(300, 100, 600, 500);
f.setLayout(new FlowLayout());
but = new Button("转到");
tf = new TextField(60);
ta = new TextArea(25, 70);
f.add(tf);
f.add(but);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void showInfo() throws Exception { //根据输入的链接地址进行获取内容
ta.setText("");
String url = tf.getText(); //http://172.16.41.154:8080/myweb/demo.html
int index1 = url.indexOf("//") + 2; //获取第一次出现的位置,然后向后移两位
int index2 = url.indexOf("/", index1);
String str = url.substring(index1, index2);
String path = url.substring(index2); //从 index2 开始截取访问的资源路径
//ta.setText(str + "...." + path);
String[] arr = str.split(":");
String host = arr[0];
int port = Integer.parseInt(arr[1]);
//ta.setText(host + "....." + port);
//建立客服端服务,向服务器发送请求
Socket s = new Socket(host, port); //指定主机和端口
InputStream in = s.getInputStream();
OutputStream out = s.getOutputStream();
PrintWriter pw = new PrintWriter(out, true);
pw.println("GET " + path + " HTTP/1.1");
pw.println("Accept: */*");
pw.println("Accept-Language: zh-cn");
pw.println("Host: 172.16.41.154:8080");
pw.println("Connection: close");
pw.println();
pw.println();
BufferedReader bufr = new BufferedReader(new InputStreamReader(in));
String line = null;
while((line = bufr.readLine()) != null) {
ta.append(line + "\r\n");
}
s.close();
}
private void myEvent() {
f.addWindowListener(new WindowAdapter() { //监控关闭frame窗口按钮
public void windowClosing(WindowEvent e) {
System.exit(1);
}
});
tf.addKeyListener(new KeyAdapter() { //文本框监听,回车转到
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_ENTER) {
try {
showInfo();
} catch(Exception ie) {}
}
}
});
but.addActionListener(new ActionListener() { //按钮监听鼠标转到
public void actionPerformed(ActionEvent e) {
try {
showInfo();
} catch(Exception ie) {}
}
});
}
}
public class MyIEByGUIDemo {
public static void main(String[] args) {
new MyIEByGUI();
}
}
TCP--本地浏览器(通过 Tomcat 服务器)
最新推荐文章于 2024-11-08 21:38:54 发布