import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class QueryIP extends JFrame {
JLabel ipLabel=new JLabel("IP:");
JTextField ipField=new JTextField();
JButton queryBtn=new JButton("查询");
JTextArea resultArea=new JTextArea();
public QueryIP(){
super("IP地址查询");
this.setSize(350,350);
this.setLayout(null);
ipLabel.setBounds(20,30,20,30);
ipField.setBounds(50,30,150,30);
queryBtn.setBounds(220,30,60,30);
resultArea.setBounds(20,80,250,200);
resultArea.setLineWrap(true);
this.add(ipLabel);
this.add(ipField);
this.add(queryBtn);
this.add(resultArea);
this.setVisible(true);
QueryListener listener=new QueryListener();
queryBtn.addActionListener(listener);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
class QueryListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String ip=ipField.getText();
if(ip.equals("")){
JOptionPane.showMessageDialog(null,"请输入IP",
"提示",JOptionPane.ERROR_MESSAGE);
return;
}
try{
URL url=new URL("http://ip.chinaz.com/");
URLConnection connect=url.openConnection();
connect.setDoOutput(true);
PrintWriter out=new PrintWriter(connect.getOutputStream());
out.write("search_ip="+ip+"&B1=%B2%E9%D1%AF");
out.close();
InputStream in=connect.getInputStream();
String content="";
byte[]bs=new byte[256];
while(in.read(bs)!=-1){
content+=new String(bs,"gb2313");
}
String[]line=content.split("\n");
String result="";
for(int i=0;i<line.length;i++){
if(line[i].indexOf("以下权威资料由国际IP管理机构apnic提供:")!=-1){
result=line[i].trim().replaceAll("\\&[a-zA-Z]{1,10}","").replaceAll("<[^>]*>","");
break;
}
}
resultArea.setText(result);
}
catch(IOException ex){
JOptionPane.showMessageDialog(null,ex.getMessage(),
"提示",JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
new QueryIP();
}
}
我替换了ip查询网址 原文的网址关闭了,换了这个网址里不能查询ip地址了 求大神帮忙
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class QueryIP extends JFrame {
JLabel ipLabel=new JLabel("IP:");
JTextField ipField=new JTextField();
JButton queryBtn=new JButton("查询");
JTextArea resultArea=new JTextArea();
public QueryIP(){
super("IP地址查询");
this.setSize(350,350);
this.setLayout(null);
ipLabel.setBounds(20,30,20,30);
ipField.setBounds(50,30,150,30);
queryBtn.setBounds(220,30,60,30);
resultArea.setBounds(20,80,250,200);
resultArea.setLineWrap(true);
this.add(ipLabel);
this.add(ipField);
this.add(queryBtn);
this.add(resultArea);
this.setVisible(true);
QueryListener listener=new QueryListener();
queryBtn.addActionListener(listener);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
}
class QueryListener implements ActionListener{
public void actionPerformed(ActionEvent e){
String ip=ipField.getText();
if(ip.equals("")){
JOptionPane.showMessageDialog(null,"请输入IP",
"提示",JOptionPane.ERROR_MESSAGE);
return;
}
try{
URL url=new URL("http://ip.chinaz.com/");
URLConnection connect=url.openConnection();
connect.setDoOutput(true);
PrintWriter out=new PrintWriter(connect.getOutputStream());
out.write("search_ip="+ip+"&B1=%B2%E9%D1%AF");
out.close();
InputStream in=connect.getInputStream();
String content="";
byte[]bs=new byte[256];
while(in.read(bs)!=-1){
content+=new String(bs,"gb2313");
}
String[]line=content.split("\n");
String result="";
for(int i=0;i<line.length;i++){
if(line[i].indexOf("以下权威资料由国际IP管理机构apnic提供:")!=-1){
result=line[i].trim().replaceAll("\\&[a-zA-Z]{1,10}","").replaceAll("<[^>]*>","");
break;
}
}
resultArea.setText(result);
}
catch(IOException ex){
JOptionPane.showMessageDialog(null,ex.getMessage(),
"提示",JOptionPane.ERROR_MESSAGE);
}
}
}
public static void main(String[] args) {
new QueryIP();
}
}
我替换了ip查询网址 原文的网址关闭了,换了这个网址里不能查询ip地址了 求大神帮忙