import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class GetMobileUserAgent {
protected boolean ifBlock;
public GetMobileUserAgent()
{
ifBlock=true;
}
public boolean blockUpBlackUa(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException
{
String userAgent = request.getHeader("User-Agent").toLowerCase();
String white_gateway[]={"12.25.203.11","202.108.235.154"};
String black_userAgent[]={"opera","msie","opmv-sdk","m3gate","up.browser"};
String remote_ip = request.getRemoteAddr();
for(int i=0;i
{
userAgent=userAgent.toLowerCase();
if(userAgent.indexOf(black_userAgent[i])>-1)
{
ifBlock=false;
i=black_userAgent.length;
}
else
continue;
}
for(int i=0;i
{
if(remote_ip.equals(white_gateway[i]))
{
i=white_gateway.length;
ifBlock=true;
}
else
continue;
}
if(!ifBlock)
{
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("
");
out.println("对不起,没有适配您的终端型号的内容。
");
out.println("梦网首页
");
}
return ifBlock;
}
public static void getUserAgent(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException, NoSuchElementException
{
Enumeration enum_header=request.getHeaderNames();
StringBuffer tmpStr=new StringBuffer("");
String String_uaAll=null;
String cur_header ="test";
PrintWriter out = response.getWriter();
String userAgent = request.getHeader("User-Agent").toLowerCase();
userAgent = userAgent.substring(0,userAgent.indexOf("/"));
String path = "d:\\product\\view_cm\\webapps\\root\\mobile_ua\\"+userAgent+ ".txt";
try{
while (enum_header.hasMoreElements())
{
cur_header = enum_header.nextElement().toString();
if(cur_header==null || cur_header.equals(""))
{
new Exception("get the header's name as null!");
}
tmpStr.append(cur_header+"\n\r") ;
Enumeration enum_value = request.getHeaders(cur_header);
while (enum_value.hasMoreElements())
{
tmpStr.append(enum_value.nextElement().toString()+"\n\r");
}
}
tmpStr.append(request.getRemoteAddr()+"\n\r");
tmpStr.append(request.getRemoteHost()+"\n\r");
tmpStr.append(request.getProtocol()+"\n\r");
String_uaAll = tmpStr.toString();
FileWriter fw=new FileWriter(path,false);
fw.write(String_uaAll);
fw.close();
}
catch (Exception e)
{e.printStackTrace();}
}
}