1.目前WEB主流服务器:Apache 、Microsoft IIS和其他
2.Java程序:
类:SocketTest.java
import java.net.*;
import java.io.*;
import java.text.*;
public class SocketTest
{
public static int[] num={0,0,0};
public static void main(String[] args)
{
try{
BufferedReader is=new BufferedReader(new FileReader("serverlist.txt"));
String server;
String format = "%1$-25s%2$-48s";
System.out.format(format,"网址","服务器信息");
System.out.println("===========================================================");
while ((server = is.readLine()) != null) {
ShowServerInfo(server.trim(),80);
}
System.out.println("===========================================================");
System.out.println("Apache:"+num[0]+",比例 "+NumberFormat.getPercentInstance().format(num[0]*1.0/(num[0]+num[1]+num[2])));
System.out.println("IIS:"+num[1]+",比例 "+NumberFormat.getPercentInstance().format(num[1]*1.0/(num[0]+num[1]+num[2])));
System.out.println("Other:"+num[2]+",比例 "+NumberFormat.getPercentInstance().format(num[2]*1.0/(num[0]+num[1]+num[2])));
is.close();
}catch(Exception ex){
ex.printStackTrace();
}
}
public static void ShowServerInfo(String server,int port){
try{
Socket sc=new Socket(server,port);
StringBuffer sb=new StringBuffer();
sb.append("HEAD / HTTP/1.1\n");
sb.append("Accept: */* \n");
sb.append("Host: "+server+"\n");
sb.append("Connection: Keep-Alive \n\n");
sc.getOutputStream().write(sb.toString().getBytes());
BufferedReader in=new BufferedReader(new InputStreamReader(sc.getInputStream()));
String userInput;
String format = "%1$-25s%2$-54s\n";
while ((userInput = in.readLine()) != null) {
if(userInput.startsWith("Server:")){
if(userInput.length()>55) userInput=userInput.substring(0,54);
if(userInput.toUpperCase().indexOf("APACHE")>=0) num[0]+=1;
else if(userInput.indexOf("IIS")>0) num[1]+=1;
else num[2]+=1;
break;
}
}
sc.getOutputStream().close();
in.close();
sc.close();
}catch(Exception ex){
System.out.println("Err:"+server+","+ex.getMessage());
}
}
}
3.所需文本:serverlist.txt
4.结果显示:
网址 服务器信息
===========================================================
www.google.com gws
gmail.google.com GSE
ditu.google.com GFE/2.0
www.sina.com nginx
www.sohu.com SWS
www.163.com Cdn Cache Server V2.0
www.263.com Apache/2.2.18 (Unix) PHP/5.3.6
www.126.com nginx
www.cctv.com Apache
www.qq.com squid/3.1.18
www.tom.com Apache
www.hao123.com BWS/1.0
www.3158.cn Apache/2.00(130515)
www.sina.com.cn nginx
www.baidu.com BWS/1.0
mail.163.com nginx
sz.gd.vnet.cn nginx/0.8.53
www.online.sh.cn Apache/2.4.6 (Unix)
www.ourgame.com Microsoft-IIS/6.0
www.taobao.com Tengine
www.sohu.com SWS
www.google.cn sffe
www.21cn.com nginx/1.2.3
www.phoenixtv.com Tengine/1.3.0
www.gznet.com Apache
www.wuhan.net.cn wuhan-online
www.jrj.com.cn Apache
hi.baidu.com apache
www.avl.com.cn Apache/2.0.53 (Unix)
www.163.com Cdn Cache Server V2.0
mail.sina.com.cn Apache
www.xinhuanet.com nginx/1.2.6
www.zhcw.com Apache/2.2.15 (Unix) PHP/5.3.0
www.hunantv.com nginx/1.0.0
www.qq.com squid/3.1.18
cn.msn.com Microsoft-IIS/7.5
www.people.com.cn RMW
【备注:本帖转载,有修改】