package socketTest;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class HttpIEDemo2 {
/**
* 需求:
* 1:服务器是tomcat,里面有一个hello.html简单的页面
* 2:客户端自定义
* 要求自定义的客户端能访问服务器的资源,通过URL对象的方法,不用Socket的对象
* 两者对比
* 这里不会出现传输层的头部信息,还有一些确认信息等
* 简易版 IE 浏览器 2.0
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyIeWeb1 ie = new MyIeWeb1();
}
}
class MyIeWeb1 extends JFrame implements ActionListener
{
private TextField tf = null;
private TextArea ta = null;
private JButton jb1 = null;
private JPanel jp1 = null;
private JPanel jp2 = null;
//初始化相关的组件及注册简听
public MyIeWeb1()
{
tf = new TextField(50);
ta = new TextArea();
jb1 = new JButton("转到");
jb1.addActionListener(this);
jb1.setActionCommand("zd");
jp1 = new JPanel();
jp2 = new JPanel();
jp1.add(tf);
jp1.add(jb1);
jp2.add(ta);
this.setLayout(new BorderLayout());
this.add(jp1,BorderLayout.NORTH);
this.add(jp2,BorderLayout.CENTER);
this.setSize(700,300);
this.setLocation(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("zd"))
{
//监听处理
showIEWeb();
}
}
public void showIEWeb()
{
// System.out.println("转到");
//每一次访问前要刷新
ta.setText("");
//得到url的值
String urlPath = tf.getText(); //http://192.168.1.154:8888/MyWebSit/hello.html
try {
//拋 MalformedURLException 异常
URL url = new URL(urlPath);
//拋IO异常
URLConnection conn = url.openConnection();
//封装了Socket对象中的getInputStream()方法
InputStream in = conn.getInputStream();
byte [] buf = new byte[1024];
int len = in.read(buf);
ta.setText(new String(buf,0,len));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.net.*;
public class HttpIEDemo2 {
/**
* 需求:
* 1:服务器是tomcat,里面有一个hello.html简单的页面
* 2:客户端自定义
* 要求自定义的客户端能访问服务器的资源,通过URL对象的方法,不用Socket的对象
* 两者对比
* 这里不会出现传输层的头部信息,还有一些确认信息等
* 简易版 IE 浏览器 2.0
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
MyIeWeb1 ie = new MyIeWeb1();
}
}
class MyIeWeb1 extends JFrame implements ActionListener
{
private TextField tf = null;
private TextArea ta = null;
private JButton jb1 = null;
private JPanel jp1 = null;
private JPanel jp2 = null;
//初始化相关的组件及注册简听
public MyIeWeb1()
{
tf = new TextField(50);
ta = new TextArea();
jb1 = new JButton("转到");
jb1.addActionListener(this);
jb1.setActionCommand("zd");
jp1 = new JPanel();
jp2 = new JPanel();
jp1.add(tf);
jp1.add(jb1);
jp2.add(ta);
this.setLayout(new BorderLayout());
this.add(jp1,BorderLayout.NORTH);
this.add(jp2,BorderLayout.CENTER);
this.setSize(700,300);
this.setLocation(400,300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getActionCommand().equals("zd"))
{
//监听处理
showIEWeb();
}
}
public void showIEWeb()
{
// System.out.println("转到");
//每一次访问前要刷新
ta.setText("");
//得到url的值
String urlPath = tf.getText(); //http://192.168.1.154:8888/MyWebSit/hello.html
try {
//拋 MalformedURLException 异常
URL url = new URL(urlPath);
//拋IO异常
URLConnection conn = url.openConnection();
//封装了Socket对象中的getInputStream()方法
InputStream in = conn.getInputStream();
byte [] buf = new byte[1024];
int len = in.read(buf);
ta.setText(new String(buf,0,len));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}