📄 searchinfoui.java
字号:
package web.http.ui;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
/**
* 提供搜索服务器的窗口
*
*/
public class SearchInfoUI extends JFrame implements ActionListener
{
JLabel labIP = new JLabel();
JTextField tfIP = new JTextField();
JLabel labInfo = new JLabel();
JTextField tfInfo = new JTextField();
JButton bSearch = new JButton();
JPanel panel = new JPanel();
JScrollPane scroll = new JScrollPane(panel);
/**
* 连接服务器的socket
*/
protected Socket client;
/**
* 数据输出,向服务器发送数据的接口
*/
protected DataOutputStream sender;
/**
* 数据输入:接受服务器 端发送的数据
*/
protected DataInputStream receiver;
/**
* 服务器发送过来的数据
*/
private String content = null;
private WebClientUI browser = null;
public SearchInfoUI()
{
initUI();
}
/**
* 初始化界面
*
*/
private void initUI()
{
panel.setBounds(new Rectangle(19, 119, 392, 166));
panel.setLayout(null);
labIP.setBorder(BorderFactory.createLineBorder(Color.black));
labIP.setText("请输入服务器IP");
labIP.setBounds(new Rectangle(18, 67, 97, 23));
this.getContentPane().setLayout(null);
this.setTitle("在服务器端搜索信息");
tfInfo.setBounds(new Rectangle(125, 30, 237, 25));
labInfo.setBorder(BorderFactory.createLineBorder(Color.black));
labInfo.setText("请输入查询内容");
labInfo.setBounds(new Rectangle(18, 31, 97, 23));
tfIP.setBounds(new Rectangle(125, 67, 236, 25));
tfIP.setToolTipText("提示:您只能在支持该搜索服务的服务器上搜索信息");
bSearch.setBorder(BorderFactory.createLineBorder(Color.black));
bSearch.setText("查找");
bSearch.setBounds(new Rectangle(378, 29, 59, 63));
scroll.setAutoscrolls(true);
scroll.setBorder(BorderFactory.createLineBorder(Color.black));
scroll.setBounds(new Rectangle(19, 119, 392, 166));
this.getContentPane().add(tfIP, null);
this.getContentPane().add(tfInfo, null);
this.getContentPane().add(labInfo, null);
this.getContentPane().add(labIP, null);
this.getContentPane().add(bSearch, null);
this.getContentPane().add(scroll, null);
bSearch.addActionListener(this);
this.setSize(450, 350);
this.setLocation(100, 100);
this.setVisible(true);
}
/**
* 事情响应代码
*/
public void actionPerformed(ActionEvent e)
{
if (e.getSource().equals(bSearch))
{
if(tfIP.getText().trim().equals(""))
{
error("请输入服务器IP地址以供搜索!");
return;
}
if(tfInfo.getText().trim().equals(""))
{
error("请输入要查找的信息!");
return;
}
search(tfIP.getText().trim(), tfInfo.getText().trim());
} else
{ //连接按键所指向的URL
accessURL(e.getActionCommand());
}
}
/**
* 访问网页
*
* @param url
*/
public void accessURL(String url)
{
if (this.browser != null)
browser.access(url);
this.setVisible(false);
}
/**
* 设置浏览器
*
* @param browser
*/
public void setBrowser(WebClientUI browser)
{
this.browser = browser;
}
/**
* 设置IP
*
* @param strIP
*/
public void setIP(String strIP)
{
this.tfIP.setText(strIP);
}
/**
* 查找信息
*
* @param ipaddress
* 服务器IP
* @param info
* 查找信息
*/
public void search(String ipaddress, String info)
{
try
{
content = "";
panel.removeAll();
sendMessage(ipaddress, info);
int response=receiveMessage();
decodingHTML(content,response);//HTML简单解析
} catch (UnknownHostException e)
{
error("无法找到服务器:" + e.getMessage());
} catch (ConnectException e)
{
error("无法连接服务器:" + e.getMessage());
} catch (IOException e)
{
error("连接服务器发生IO错误:" + e.getMessage());
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 显示错误信息
*
* @param errorInfo
*/
private synchronized void error(String errorInfo)
{
JOptionPane.showMessageDialog(this, errorInfo);
}
/**
* 向服务器发送消息
*/
public void sendMessage(String ipaddress, String info) throws Exception
{
client = new Socket(ipaddress, 80);
sender = new DataOutputStream(client.getOutputStream());
receiver = new DataInputStream(client.getInputStream());
String headInfo = "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*\r\n"
+ "Accept-Language: zh-cn\r\n"
+ "Accept-Encoding: gzip, deflate\r\n"
+ "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Maxthon)\r\n"
+ "Host:" + ipaddress + "\r\n" + "Connection: Keep-Alive\r\n";
String data = "GET /?" + info + " HTTP/1.1\r\n" + headInfo + "\r\n";
sender.write(data.getBytes(), 0, data.length());
sender.flush();
}
/**
* 接收来自HTTP服务器的数据
*/
protected int receiveMessage() throws Exception
{
String aline;
aline = receiver.readLine();
if(aline==null||aline.length()<=3)
{
closeServer();//发送错误
return -1;
}
else if(aline.indexOf("200")<0)
{
closeServer();//该服务器不支持该功能
return 0;
}
while ((aline = receiver.readLine()) != null)
content += aline;
closeServer();
return 1;
}
/**
* 提取HTML标记中的 连接 标记
*
* @param htmlStr
*/
protected void decodingHTML(String htmlStr,int response)
{
String temp = htmlStr;
if(response==0)
{
JLabel lab=new JLabel("该服务器不支持该查找功能");
lab.setBounds(100, 20, 200, 25);
panel.add(lab);
return ;
}
int num = 0;
while (temp != null && temp.length() > 0)
{//提取返回页面信息中查找到的URL地址
int index = temp.indexOf("<a href=");//URL地址开始处
if (index < 0)
break;
temp = temp.substring(index + 8);
int endIndex = temp.indexOf(">");//URL地址结束处
if (endIndex > 0)
{
String href = temp.substring(0, endIndex);//URL地址
JButton b = new JButton(href);//为每一个URL地址创建一个按钮
b.setBounds(5, 5 + num * 30, 380, 25);
b.setBorder(BorderFactory.createLineBorder(Color.black));
num++;
b.addActionListener(this);
panel.add(b);
} else
break;
temp = temp.substring(endIndex + 1);
}
if(num==0)
{
JLabel lab=new JLabel("服务器上没有找到该信息");
lab.setBounds(100, 20, 200, 25);
panel.add(lab);
}
this.setVisible(false);
this.setVisible(true);
}
/**
* 关闭连接 同时关闭数据流
*/
protected void closeServer() throws Exception
{
if (client == null)
return;
try
{
client.close();
sender.close();
receiver.close();
} catch (IOException ioe)
{
throw ioe;
}
client = null;
sender = null;
receiver = null;
}
public static void main(String[] args)
{
new SearchInfoUI();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -