分享

移动资费提示过滤

 CevenCheng 2010-10-15
最近移动对网关进行改造,会弹出一个资费提示的wap页面,干扰了众多手机应用,手机网络游戏的正常运行,仅提供一个移动资费提示过滤的工具类代码,以抛砖引玉 

Java代码 
  1. package redduke.game.j2me.io;  
  2.   
  3. import java.io.ByteArrayOutputStream;  
  4. import java.io.DataInputStream;  
  5. import java.io.DataOutputStream;  
  6. import java.io.IOException;  
  7. import java.io.InputStream;  
  8. import java.io.OutputStream;  
  9. import java.util.Enumeration;  
  10. import java.util.Hashtable;  
  11.   
  12. import javax.microedition.io.Connector;  
  13. import javax.microedition.io.HttpConnection;  
  14.   
  15. public class HttpUtil{  
  16.     /* 
  17.      * 缺省过滤器实例 
  18.      */  
  19.     protected static HttpConnectionFilter filter=new HttpConnectionFilter()  
  20.     {  
  21.         public boolean accept(HttpConnection con) {  
  22.             /* 资费提示wap页面长度=438+目的地URL字符串字节长度 */  
  23.             return con!=null && con.getLength()!=438+con.getURL().getBytes().length;  
  24.         }  
  25.     };  
  26.     /** 
  27.      * 打开一个HttpConnection 
  28.      * @param url 
  29.      */  
  30.     public static HttpConnection open(final String url)  
  31.     {  
  32.         /* 一个HttpConnection的代理实现 */  
  33.         return new HttpConnection()  
  34.         {  
  35.             /* 
  36.              * 连接 
  37.              */  
  38.             private javax.microedition.io.HttpConnection con;  
  39.                           
  40.             /* 
  41.              * 连接次数,超过次数则认为连接失败 
  42.              */  
  43.             private int connectCount=0;  
  44.             /* 
  45.              * 输出缓冲 
  46.              */  
  47.             private ByteArrayOutputStream baos=new ByteArrayOutputStream(1024);  
  48.               
  49.             /* 
  50.              * 请求属性 
  51.              */  
  52.             private Hashtable requestProperties=new Hashtable();  
  53.               
  54.             /* 
  55.              * 请求方式 
  56.              */  
  57.             private String method=GET;  
  58.                           
  59.             /* 
  60.              * 连接服务器 
  61.              */  
  62.             private void connect()  
  63.             {  
  64.                 try  
  65.                 {  
  66.                     if(this.con!=null)  
  67.                         this.con.close();  
  68.                 }  
  69.                 catch(Exception ex){}  
  70.                 try  
  71.                 {  
  72.                     this.con=(javax.microedition.io.HttpConnection)Connector.open(url);  
  73.                     this.con.setRequestMethod(this.method);  
  74.                     Enumeration propertyKeys=this.requestProperties.keys();  
  75.                     while(propertyKeys.hasMoreElements())  
  76.                     {  
  77.                         String propertyKey=(String)propertyKeys.nextElement();  
  78.                         String propertyValue=(String)requestProperties.get(propertyKey);  
  79.                         this.con.setRequestProperty(propertyKey,propertyValue);  
  80.                     }  
  81.                 }  
  82.                 catch(Exception ex)  
  83.                 {  
  84.                     this.con=null;  
  85.                 }  
  86.             }     
  87.             /* 
  88.              * 确认连接 
  89.              */  
  90.             private void ensureConnect()  
  91.             {  
  92.                 while(this.con==null || filter!=null && !filter.accept(this.con))  
  93.                 {  
  94.                     if(connectCount>=5)  
  95.                         throw new RuntimeException("Connection failed");  
  96.                     connectCount++;  
  97.                     this.connect();  
  98.                 }  
  99.             }  
  100.               
  101.             public long getDate() throws IOException {  
  102.                 ensureConnect();  
  103.                 return this.con.getDate();  
  104.             }  
  105.   
  106.             public long getExpiration() throws IOException {  
  107.                 ensureConnect();  
  108.                 return this.con.getExpiration();          
  109.             }  
  110.   
  111.             public String getFile() {  
  112.                 ensureConnect();  
  113.                 return this.con.getFile();        
  114.             }  
  115.   
  116.             public String getHeaderField(String arg0) throws IOException {  
  117.                 ensureConnect();  
  118.                 return this.con.getHeaderField(arg0);  
  119.             }  
  120.   
  121.             public String getHeaderField(int arg0) throws IOException {  
  122.                 ensureConnect();  
  123.                 return this.con.getHeaderField(arg0);  
  124.             }  
  125.   
  126.             public long getHeaderFieldDate(String arg0, long arg1) throws IOException {  
  127.                 ensureConnect();  
  128.                 return this.con.getHeaderFieldDate(arg0, arg1);  
  129.             }  
  130.   
  131.             public int getHeaderFieldInt(String arg0, int arg1) throws IOException {  
  132.                 ensureConnect();  
  133.                 return this.con.getHeaderFieldInt(arg0, arg1);  
  134.             }  
  135.   
  136.             public String getHeaderFieldKey(int arg0) throws IOException {  
  137.                 ensureConnect();  
  138.                 return this.con.getHeaderFieldKey(arg0);  
  139.             }  
  140.   
  141.             public String getHost() {  
  142.                 ensureConnect();  
  143.                 return this.con.getHost();  
  144.             }  
  145.   
  146.             public long getLastModified() throws IOException {  
  147.                 ensureConnect();  
  148.                 return this.con.getLastModified();  
  149.             }  
  150.   
  151.             public int getPort() {  
  152.                 ensureConnect();  
  153.                 return this.con.getPort();  
  154.             }  
  155.   
  156.             public String getProtocol() {  
  157.                 ensureConnect();  
  158.                 return this.con.getProtocol();  
  159.             }  
  160.   
  161.             public String getQuery() {  
  162.                 ensureConnect();  
  163.                 return this.con.getQuery();  
  164.             }  
  165.   
  166.             public String getRef() {  
  167.                 ensureConnect();  
  168.                 return this.con.getRef();  
  169.             }  
  170.   
  171.             public String getRequestMethod() {  
  172.                 ensureConnect();  
  173.                 return this.con.getRequestMethod();       
  174.             }  
  175.   
  176.             public String getRequestProperty(String arg0) {  
  177.                 ensureConnect();  
  178.                 return this.con.getRequestProperty(arg0);  
  179.             }  
  180.   
  181.             public int getResponseCode() throws IOException {  
  182.                 ensureConnect();  
  183.                 return this.con.getResponseCode();  
  184.             }  
  185.   
  186.             public String getResponseMessage() throws IOException {  
  187.                 ensureConnect();  
  188.                 return this.con.getResponseMessage();  
  189.             }  
  190.   
  191.             public String getURL() {  
  192.                 ensureConnect();  
  193.                 return this.con.getURL();  
  194.             }     
  195.             public void setRequestMethod(String arg0) throws IOException {  
  196.                 this.method=arg0;  
  197.             }  
  198.               
  199.             public void setRequestProperty(String arg0, String arg1) throws IOException {  
  200.                 requestProperties.put(arg0, arg1);  
  201.             }  
  202.   
  203.             public String getEncoding() {  
  204.                 ensureConnect();  
  205.                 return this.con.getEncoding();  
  206.             }  
  207.   
  208.             public long getLength() {  
  209.                 ensureConnect();  
  210.                 return this.con.getLength();  
  211.             }  
  212.   
  213.             public String getType() {  
  214.                 ensureConnect();  
  215.                 return this.con.getType();  
  216.             }  
  217.   
  218.             public DataInputStream openDataInputStream() throws IOException {  
  219.                 ensureConnect();  
  220.                 return this.con.openDataInputStream();  
  221.             }  
  222.   
  223.             public InputStream openInputStream() throws IOException {  
  224.                 ensureConnect();  
  225.                 return this.con.openInputStream();  
  226.             }  
  227.             public void close() throws IOException {  
  228.                 ensureConnect();  
  229.                 this.con.close();  
  230.             }     
  231.             public DataOutputStream openDataOutputStream() throws IOException {  
  232.                 return new DataOutputStream(baos);  
  233.             }     
  234.             public OutputStream openOutputStream() throws IOException {  
  235.                 return baos;  
  236.             }  
  237.         };  
  238.     }  
  239.     /* 
  240.      * 设置过滤器,可设置自定义过滤器 
  241.      */  
  242.     public static void setHttpConnectionFilter(HttpConnectionFilter filter)  
  243.     {  
  244.         HttpUtil.filter=filter;  
  245.     }  
  246.       
  247.     /** 
  248.      * 过滤器接口 
  249.      */  
  250.     public static interface HttpConnectionFilter  
  251.     {  
  252.         boolean accept(HttpConnection con);  
  253.     }  
  254. }  


使用例子 

Java代码 
  1. HttpConnection con=HttpUtil.open("http:\\abc.com");  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约