zoukankan      html  css  js  c++  java
  • java 获取手机归属地,引起net.UnknownHostException错误

    这个问题是请求,重定向了,跟入源码。修改了地址,变成302

                    Connection connect = Jsoup.connect(url);
    	        connect.header("Host", "http://info.bet007.com");
    	        connect.header("User-Agent", "  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0");
    	        connect.header("Accept", "  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    	        connect.header("Accept-Language", "zh-cn,zh;q=0.5");
    	        connect.header("Accept-Charset", "  GB2312,utf-8;q=0.7,*;q=0.7");
    	        connect.header("Connection", "keep-alive");
    			
    	        Document doc = connect.get();
    

      就可以获取到手机归属地了

    	public static String[] getPhoneAddressAndCompanyStrs(String phone){
    		if(phone.length()<=0)
    			return null;
    		String url = "http://www.ip138.com:8080/search.asp?action=mobile&mobile=%s";
    		url = String.format(url, phone);
    		
    		String[] strs=new String[2];
    		try {
    			Connection connect = Jsoup.connect(url);
    	        connect.header("Host", "http://info.bet007.com");
    	        connect.header("User-Agent", "  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20100101 Firefox/5.0");
    	        connect.header("Accept", "  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
    	        connect.header("Accept-Language", "zh-cn,zh;q=0.5");
    	        connect.header("Accept-Charset", "  GB2312,utf-8;q=0.7,*;q=0.7");
    	        connect.header("Connection", "keep-alive");
    			
    	        Document doc = connect.get();
    			
    			Elements els = doc.getElementsByClass("tdc2");
    			int elsSize = els.size();
    			if(elsSize == 2) {
    				strs[0]=els.get(1).text();
    			}else if(elsSize > 2) {
    				strs[0]=els.get(1).text();
    				strs[1]=els.get(2).text();
    			}
    			return strs;
    		} catch (IOException e) {
    			
    			e.printStackTrace();
    			return null;
    		}
    	}
    

      

  • 相关阅读:
    postman使用
    web应用服务器性能监控及调优
    软件测试的相关网站
    web测试点梳理
    HTTP协议详解
    Fidder详解之get和post请求
    浅谈HTTPS协议
    APP测试基本流程
    Python 拷贝对象(深拷贝deepcopy与浅拷贝copy)
    为学日益,为道日损
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/11024957.html
Copyright © 2011-2022 走看看