黑马程序员技术交流社区
标题:
在GUI中实现浏览器功能,的问题
[打印本页]
作者:
gerenvip
时间:
2013-4-25 16:17
标题:
在GUI中实现浏览器功能,的问题
本帖最后由 gerenvip 于 2013-4-25 16:18 编辑
在毕老师的24天视频中,有一个在gui图形界面中实现浏览器功能的示例中,通过简单移植,实现获取网页。
但是我自己试验时总是获取不到网页,而获取到的内容为:
“<html><head><title>Apache Tomcat/6.0.36 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 404 - /myweb/demo.htmlHTTP/1.1</h1><HR size="1" noshade="noshade"><p><b>type</b> Status report</p><p><b>message</b> <u>/myweb/demo.htmlHTTP/1.1</u></p><p><b>description</b> <u>The requested resource is not available.</u></p><HR size="1" noshade="noshade"><h3>Apache Tomcat/6.0.36</h3></body></html>”
而不是网页的内容。请高手帮忙解答一下。下边是移植的代码
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
class MyIEByGUI
{
private Frame f;
private TextField tf;
private Button but;
private TextArea ta;
private Dialog d;
private Label lab;
private Button okbut;
MyIEByGUI()
{
init();
}
public void init()
{
f = new Frame("my window");
f.setBounds(300,100,600,500);
f.setLayout(new FlowLayout());
tf=new TextField(60);
but = new Button("转到");
ta = new TextArea(20,70);//行 列
d= new Dialog(f,"提示信息-self",true); //为true,弹窗不取消掉,没法操作,
//false时,不取消弹窗也可以操作
d.setBounds(400,200,400,100);
d.setLayout(new FlowLayout());
lab = new Label();
okbut = new Button("确定");
d.add(lab);
d.add(okbut);
f.add(tf);
f.add(but);
f.add(ta);
myEvent();
f.setVisible(true);
}
private void myEvent()
{
okbut.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
d.setVisible(false);
}
});
d.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
d.setVisible(false);
}
});
//实现enter代替按钮功能。
tf.addKeyListener(new KeyAdapter()
{
public void keyPressed(KeyEvent e)
{
try
{
if(e.getKeyCode()==KeyEvent.VK_ENTER)
showDir();
}
catch (Exception el)
{
}
}
});
but.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
showDir();
}
catch (Exception ex)
{
}
}
});
f.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void showDir() throws Exception
{
ta.setText("");
String url = tf.getText();//http://192.168.0.100:8080/myweb/demo.html
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);
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: 192.168.0.100:11000");
out.println("Host: 192.168.0.100:11000");
out.println("Connection: closed");
out.println();
out.println();
BufferedReader bufr = new BufferedReader(new InputStreamReader(s.getInputStream()));
String line = null;
while((line = bufr.readLine())!=null)
{
//System.out.println(line);
ta.append(line+"\r\n");
}
s.close();
}
public static void main(String[] args)
{
new MyIEByGUI();
}
}
复制代码
作者:
孙胜
时间:
2013-4-27 22:08
你的Tomcat服务器web目录有 /myweb/demo.htm文件不,404错误是说没有找到这个文件
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2