package com.topking.query;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.UnsupportedLookAndFeelException;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
/**
*
* @author lzkj
*/
public class IPQueryFrame extends javax.swing.JFrame {
/** Creates new form PhoneQueryFrame */
public IPQueryFrame() {
setTitle("IP归属地查询器(Java版) @Author:jacky ");
setLocation(200,300);
setResizable(false);
this.setIconImage(this.getToolkit().createImage(this.getClass().getClassLoader().getResource("com/topking/query/image/logo.gif")));
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
T_phone = new javax.swing.JTextField();
bt_query = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
T_area = new javax.swing.JTextArea();
L_img = new javax.swing.JLabel(new ImageIcon(this.getClass().getClassLoader().getResource("com/topking/query/image/spinner.gif")));
L_txt = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
bt_query.setText("查询");
T_area.setColumns(20);
T_area.setRows(5);
jScrollPane1.setViewportView(T_area);
L_txt.setText("正在查询,请稍等……");
L_img.setVisible(false);
L_txt.setVisible(false);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(L_img, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(L_txt, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE))
.addGroup(layout.createSequentialGroup()
.addComponent(T_phone, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
.addGap(18, 18, 18)
.addComponent(bt_query))
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 382, Short.MAX_VALUE))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(bt_query)
.addComponent(T_phone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(L_img, javax.swing.GroupLayout.DEFAULT_SIZE, 16, Short.MAX_VALUE)
.addComponent(L_txt))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
bt_query.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
String phone = T_phone.getText().trim();
isSet = true;
if(phone!=null&&!"".equals(phone)){
T_area.setText("");
new Thread(new UpdateUI()).start();
new Thread(new queryThread(phone)).start();
isSet = false;
}else{
JOptionPane.showMessageDialog(null,"请输入要查询的IP地址", "提示",JOptionPane.ERROR_MESSAGE);
}
}
});
pack();
}// </editor-fold>
public class UpdateUI implements Runnable{
@Override
public void run() {
// TODO Auto-generated method stub
// while(true){
System.out.println("isSet Value : "+isSet);
L_img.setVisible(true);
L_txt.setVisible(true);
// L_img.setVisible(isSet==true?true:false);
// L_txt.setVisible(isSet==true?true:false);
// }
}
}
public class queryThread implements Runnable{
private String qIP;
public queryThread(String ip){
qIP = ip;
}
public void run() {
getArea(qIP);
}
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
try {
javax.swing.UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
com.birosoft.liquid.LiquidLookAndFeel.setLiquidDecorations(true);
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new IPQueryFrame().setVisible(true);
}
});
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedLookAndFeelException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static HttpMethod getGetMethod(String phone){
return new GetMethod("//features/ip.jsp?query="+phone);
}
/**
* 使用POST方式提交数据
* @return
*/
private static HttpMethod getPostMethod(String phone){
PostMethod post = new PostMethod("/features/ip.jsp");
NameValuePair simcard = new NameValuePair("query",phone);
post.setRequestBody(new NameValuePair[] { simcard});
return post;
}
public String getArea(String phone){
String result = "";
try{
HttpClient client = new HttpClient();
// client.getHostConfiguration().setHost("www.imobile.com.cn", 80, "http");
client.getHostConfiguration().setHost("www.sogou.com",80,"http");
HttpMethod method = getPostMethod(phone);//使用POST方式提交数据
client.executeMethod(method);
//打印服务器返回的状态
System.out.println(method.getStatusLine());
//打印结果页面
String response = method.getResponseBodyAsString();
// String response = new String(method.getResponseBodyAsString().getBytes("8859_1"));
//打印返回的信息
int index = response.indexOf("所在位置:");
response = response.substring(index,response.length());
int index
评论0
最新资源