您的位置:首页 > 其它

手机终端UA信息收集------- X-WAP-PROFILE

2016-07-14 20:29 369 查看
手机终端UA信息收集

取得UA信息首先想到得就是X-WAP-PROFILE。里面包含的信息真的很多,像我们关心的屏幕大小,屏幕显示字符个数,厂家、版本、浏览器型号等。

X-WAP-PROFILE在httpquest中存在是有三种可能。

request.getHeader("x-wap-profile")

request.getHeader("X-Wap-Profile")

request.getHeader("X-WAP-PROFILE")

同时 x-wap-profile文件也有xml和rdf两种格式,

 不过解析方式可以完全一样

同时x-wap-profile里面存的可能根本不是xml或者RDF格式文件

http://www.sunplusmm.com  这是LG-KU990的x-wap-profile里面存的指向地址

同时x-wap-profile里面的存的信息和useragent存的内容也有小小差异,比如多个空格 加个商家名称之内的

x-wap-profile 具体属性的含义在我空间中有写到

http://84311564.qzone.qq.com

不过需要注意的是在httpquest.header中存的值只是一个引用地址,需要load下来才是我们需要的东西,由于某些原因我未测试山寨机是否有X-WAP-PROFILE的值,不过如果只是对屏幕大小感兴趣是可以通过JS来取到的,只用把数据回传给服务器,然后通过httpquest.header中的User-agent就可以完善UA库了,其实很多时候屏幕宽度都不是限制布局的因素,重要的是屏幕显示字符数。

对于x-wap-profile的操作也可以去我空间看看,其实无非是解析XML可以根据需要来选择解析方式,程序写的不好请指正。

诺基亚 6230的x-wap-profile:

http://nds1.nds.nokia.com/uaprof/N6230r200.xml

抽出几个我认为重要的信息:

<prf:Model>                                                           手机型号

<prf:ScreenSize>                                                   屏幕大小

<prf:ScreenSizeChar>                                           屏幕显示字符数

<prf:Vendor>                                                         手机厂家

<prf:BrowserName>                                              浏览器版本

附上解析x-wap-profile的代码:

import org.w3c.dom.Document;

import org.w3c.dom.NodeList;

import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import java.io.*;

import java.util.*;

import java.net.*;

public class LoadUaLog{

 public static void main(String[] args){

  loadUaLog();

 }

  

 public static void loadUaLog(){

 try{ 

  String logPath ="/usr/lw/xml/log";

  File file = new File(logPath);

  String[] strs = file.list();

  int max = strs.length;

  for (int i =0;i<max;i++){

   if(strs[i].endsWith(".csv")){

    loadLog(logPath+"/",strs[i]);

   }

   

  }

 }catch(Exception e){

 

 }

 }

 

 public static void loadLog(String path ,String name){

 try{

System.out.println("正在解析UA的x-wap-profile的指向文件");

  File file = new File(path+name);

  BufferedReader br = new BufferedReader(new FileReader(file));

  String[] strs=new String[2];

  String str ;

  int i =0;

  while((str = br.readLine())!=null){

   str = str.replaceAll("\"","");

   str = str.trim();

   Boolean flag= str!=null&&str.length()>0;

   strs = str.split("\t");

   if(flag){

    try{

     System.out.println("开始解析x-wap-profile!");

     loadXml(strs);

     System.out.println("下载x-wap-profile成功");

    }catch(Exception e){

     System.out.println("下载x-wap-profile失败!");

    }

   }

  }

System.out.println("Ua的x-wap-profile的指向解析完成");

 }catch(IOException e){

  System.out.println("Ua的x-wap-profile的指向解析失败!");

 }

 

 }

 

 public static void loadXml(String[] xmlUrl){

  URL url = null;

  URLConnection urlConnection = null;

  HttpURLConnection httpURLConnection = null;

  InputStream ips = null;

  StringBuffer sb = new StringBuffer();

  FileWriter fw =null;

  String filename= xmlUrl[1].substring(xmlUrl[1].lastIndexOf("/"));

  String path ="/usr/lw/xml/wapprofile/"+filename;

  filename=filename.replaceAll("/","");

  File file = new File(path);

  if(!file.exists()&&(filename.endsWith(".xml")||filename.endsWith(".rdf"))){

   try{

    System.out.println("传入地址"+xmlUrl[1]);

    url  = new URL(xmlUrl[1].replaceAll(" ","%20"));

    urlConnection = url.openConnection();

//因为是从网络中下载文件,存在很多不定因素,设置一个超时时间好点。。

    urlConnection.setConnectTimeout(3000);

    urlConnection.setReadTimeout(3000);

    httpURLConnection =(HttpURLConnection)urlConnection;

    ips = httpURLConnection.getInputStream();

    fw = new FileWriter("/usr/lw/xml/wapprofile/"+filename);

    BufferedReader br = new BufferedReader(new InputStreamReader(ips));

    String inputLine;

    while((inputLine=br.readLine())!=null){

     sb.append(inputLine);

     sb.append("\n");

    }

  System.out.println("正在下载x-wap-profile:"+filename);

    fw.write(sb.toString());

    fw.write("<!-- "+ xmlUrl[0] +" -->");

   }catch(IOException e){

    System.out.println("输入流错误!传入url为"+xmlUrl[1]);

   }finally{

    try{

     fw.close();

    }catch(IOException e){

     System.out.println("输入流关闭错误!正在写的文件为/usr/lw/xml/wapprofile/"+filename);

    }

   }

   readUaXml(path,xmlUrl[0].toString());

  }else{

   System.out.println(filename+"的x-wap-profile已存在!");

  }

 }

 public static void readUaXml(String path , String ua){

  String[] strs = loadUaXml(path);

  FileWriter fw = null;

  try{ 

         fw = new FileWriter("/usr/lw/xml/ua/ua.csv",true);

System.out.println("正在将"+ua+"的x-wap-profile的UA信息写入文件");

   fw.write("weight--"+strs[0]+"\t heigth--"+strs[1]+"\t model--"+strs[2]+"\t vendor--"+strs[3]+"\t browserName--"+strs[4]+"\t ua--"+ua);

   fw.write("\n");

System.out.println(ua+"的x-wap-profile的UA信息写入文件完成");

  } catch(IOException e){

   System.out.println("写入UA信息失败");

  }catch(Exception e){

   System.out.println("其他异常引起错误");

  }

  finally{

   try{

    fw.close();

   }catch(IOException ie){

    System.out.println("关闭输入流失败!");

   }

  }

 

 }

 /*信息的字符数组

  * index=0 存放终端屏宽

  * index=1 存放终端屏高

  * index=2 存放终端型号

  * index=3 存放终端商家

  * index=4 存放终端浏览器版本

  * */

 public static String[] loadUaXml(String path){

  DocumentBuilderFactory  factory = DocumentBuilderFactory.newInstance();

  String[] strs = new String[5];

  String fileName =path.substring(path.lastIndexOf("/"));

System.out.println(path);

System.out.println("正在解析x-wap-profile:"+fileName);

          DocumentBuilder docBuilder;

  try {

   docBuilder = factory.newDocumentBuilder();

         File file=new File(path);

         Document doc = docBuilder.parse (file);

         doc.getDocumentElement ().normalize ();

         NodeList listOfUa= doc.getElementsByTagName("prf:Model");

         NodeList listOfScreenSize= doc.getElementsByTagName("prf:ScreenSize");

         NodeList listOfVendor= doc.getElementsByTagName("prf:Vendor");

  NodeList listOfBrowser =doc.getElementsByTagName("prf:BrowserName");

         if(listOfScreenSize.getLength()>0){

          String str = listOfScreenSize.item(0).getTextContent();

          String[] stri = str.split("x");

    //存放终端屏宽

    strs[0]=stri[0];

    //存放终端屏高

    strs[1]=stri[1];

         }

         if(listOfUa!=null&&listOfUa.getLength()>0){

          //存放终端型号 

          strs[2]=listOfUa.item(0).getTextContent();

         }

         if(listOfVendor!=null&&listOfVendor.getLength()>0){

          //存放终端商家

          strs[3]=listOfVendor.item(0).getTextContent();

         }

         if(listOfBrowser!=null&&listOfBrowser.getLength()>0){

          //存放终端浏览器版本

          strs[4]=listOfBrowser.item(0).getTextContent();

         }

  } catch (ParserConfigurationException e) {

   System.out.println("x-wap-profile:"+fileName+"格式解析失败!");

  } catch (IOException ie){

   System.out.println("x-wap-profile:"+fileName+"读取失败!");

  } catch (SAXException se){

   System.out.println("error");

  }catch(Exception e){

   System.out.println("未知异常!");

  }

System.out.println(strs[0]);

System.out.println("x-wap-profile:"+fileName+"解析完成!");

  return strs;

 }

}

出处:http://blog.csdn.net/lwym010278/archive/2009/07/06/4324881.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: