论坛首页 移动开发技术论坛

移动资费提示过滤

浏览 7333 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-05-18  
最近移动对网关进行改造,会弹出一个资费提示的wap页面,干扰了众多手机应用,手机网络游戏的正常运行,仅提供一个移动资费提示过滤的工具类代码,以抛砖引玉

package redduke.game.j2me.io;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;

public class HttpUtil{
	/*
	 * 缺省过滤器实例
	 */
	protected static HttpConnectionFilter filter=new HttpConnectionFilter()
	{
		public boolean accept(HttpConnection con) {
			/* 资费提示wap页面长度=438+目的地URL字符串字节长度 */
			return con!=null && con.getLength()!=438+con.getURL().getBytes().length;
		}
	};
	/**
	 * 打开一个HttpConnection
	 * @param url
	 */
	public static HttpConnection open(final String url)
	{
		/* 一个HttpConnection的代理实现 */
		return new HttpConnection()
		{
			/*
			 * 连接
			 */
			private javax.microedition.io.HttpConnection con;
						
			/*
			 * 连接次数,超过次数则认为连接失败
			 */
			private int connectCount=0;
			/*
			 * 输出缓冲
			 */
			private ByteArrayOutputStream baos=new ByteArrayOutputStream(1024);
			
			/*
			 * 请求属性
			 */
			private Hashtable requestProperties=new Hashtable();
			
			/*
			 * 请求方式
			 */
			private String method=GET;
						
			/*
			 * 连接服务器
			 */
			private void connect()
			{
				try
				{
					if(this.con!=null)
						this.con.close();
				}
				catch(Exception ex){}
				try
				{
					this.con=(javax.microedition.io.HttpConnection)Connector.open(url);
					this.con.setRequestMethod(this.method);
					Enumeration propertyKeys=this.requestProperties.keys();
					while(propertyKeys.hasMoreElements())
					{
						String propertyKey=(String)propertyKeys.nextElement();
						String propertyValue=(String)requestProperties.get(propertyKey);
						this.con.setRequestProperty(propertyKey,propertyValue);
					}
				}
				catch(Exception ex)
				{
					this.con=null;
				}
			}	
			/*
			 * 确认连接
			 */
			private void ensureConnect()
			{
				while(this.con==null || filter!=null && !filter.accept(this.con))
				{
					if(connectCount>=5)
						throw new RuntimeException("Connection failed");
					connectCount++;
					this.connect();
				}
			}
			
			public long getDate() throws IOException {
				ensureConnect();
				return this.con.getDate();
			}

			public long getExpiration() throws IOException {
				ensureConnect();
				return this.con.getExpiration();		
			}

			public String getFile() {
				ensureConnect();
				return this.con.getFile();		
			}

			public String getHeaderField(String arg0) throws IOException {
				ensureConnect();
				return this.con.getHeaderField(arg0);
			}

			public String getHeaderField(int arg0) throws IOException {
				ensureConnect();
				return this.con.getHeaderField(arg0);
			}

			public long getHeaderFieldDate(String arg0, long arg1) throws IOException {
				ensureConnect();
				return this.con.getHeaderFieldDate(arg0, arg1);
			}

			public int getHeaderFieldInt(String arg0, int arg1) throws IOException {
				ensureConnect();
				return this.con.getHeaderFieldInt(arg0, arg1);
			}

			public String getHeaderFieldKey(int arg0) throws IOException {
				ensureConnect();
				return this.con.getHeaderFieldKey(arg0);
			}

			public String getHost() {
				ensureConnect();
				return this.con.getHost();
			}

			public long getLastModified() throws IOException {
				ensureConnect();
				return this.con.getLastModified();
			}

			public int getPort() {
				ensureConnect();
				return this.con.getPort();
			}

			public String getProtocol() {
				ensureConnect();
				return this.con.getProtocol();
			}

			public String getQuery() {
				ensureConnect();
				return this.con.getQuery();
			}

			public String getRef() {
				ensureConnect();
				return this.con.getRef();
			}

			public String getRequestMethod() {
				ensureConnect();
				return this.con.getRequestMethod();		
			}

			public String getRequestProperty(String arg0) {
				ensureConnect();
				return this.con.getRequestProperty(arg0);
			}

			public int getResponseCode() throws IOException {
				ensureConnect();
				return this.con.getResponseCode();
			}

			public String getResponseMessage() throws IOException {
				ensureConnect();
				return this.con.getResponseMessage();
			}

			public String getURL() {
				ensureConnect();
				return this.con.getURL();
			}	
			public void setRequestMethod(String arg0) throws IOException {
				this.method=arg0;
			}
			
			public void setRequestProperty(String arg0, String arg1) throws IOException {
				requestProperties.put(arg0, arg1);
			}

			public String getEncoding() {
				ensureConnect();
				return this.con.getEncoding();
			}

			public long getLength() {
				ensureConnect();
				return this.con.getLength();
			}

			public String getType() {
				ensureConnect();
				return this.con.getType();
			}

			public DataInputStream openDataInputStream() throws IOException {
				ensureConnect();
				return this.con.openDataInputStream();
			}

			public InputStream openInputStream() throws IOException {
				ensureConnect();
				return this.con.openInputStream();
			}
			public void close() throws IOException {
				ensureConnect();
				this.con.close();
			}	
			public DataOutputStream openDataOutputStream() throws IOException {
				return new DataOutputStream(baos);
			}	
			public OutputStream openOutputStream() throws IOException {
				return baos;
			}
		};
	}
	/*
	 * 设置过滤器,可设置自定义过滤器
	 */
	public static void setHttpConnectionFilter(HttpConnectionFilter filter)
	{
		HttpUtil.filter=filter;
	}
	
	/**
	 * 过滤器接口
	 */
	public static interface HttpConnectionFilter
	{
		boolean accept(HttpConnection con);
	}
}



使用例子

HttpConnection con=HttpUtil.open("http:\\abc.com");
   发表时间:2007-05-21  
ps:缺省的过滤器中的关键数值438是经过测试得来的,这个过滤器实现比较简单,而且不需要读取内容下来进行判断,降低了信息流量,可根据具体应用自行实现HttpConnectionFilter接口,并设置
0 请登录后投票
   发表时间:2007-05-21  
楼主在哪个省啊?不是全国的状况都是这样的吧?
0 请登录后投票
   发表时间:2007-05-22  
protected static HttpConnectionFilter filter=new HttpConnectionFilter()   
    {   
        public boolean accept(HttpConnection con) {   
            /* 资费提示wap页面长度=438+目的地URL字符串字节长度 */  
            return con!=null && con.getLength()!=438+con.getURL().getBytes().length;   
        }   
    };
0 请登录后投票
   发表时间:2007-05-25  
请问,你这个HttpConnection con=HttpUtil.open("http:\\abc.com");   放在哪儿,是不是我的过滤器就执行这段代码?谢谢
0 请登录后投票
   发表时间:2007-08-25  
其实简单的过滤方法是:在网络传送回来的数据流中,判断是否有你访问的网络地址数据串,如果有,就是资费页面,这时候再重新访问一次即可获得正确的数据。我的程序中就是这么做的,应该比较通用吧!
0 请登录后投票
论坛首页 移动开发技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics