通常在服务器与客户机之间,我们使用HTTP协议通过浏览器与服务器之间进行通信,查阅了书籍发现服务器是通过GET命令接受客户机请求的。


[img]/upload/attachment/122093/0e514cf1-3484-391f-b496-6965c124986e.jpg[/img]

反正不管什么,只要服务器与客户机建立一次TCP连接,使用HTTP的GET就可以让服务器传数据给客户机。问题在这,资料上说,HTML中的图片并不是与第一次GET连接一起传过来的。比如:你第一次向服务器GET一下,服务器响应,把HTML页面代码传给你,之后浏览器开始解释这些代码,当浏览器发现传过来的代码中有图片时,它会自己再次向服务器发送GET请求,此时你虽然没有手动发送GET,但实际上已经GET了。这样一来,对WEB服务器必然带来比较多的负载,所以,处理图片传送的最好使用单独的文件服务器。当然,GET传过来的还有一些机器信息等等,这里就不多说了。补充一下,这里的命令一共有三个,一个是GET,一个是POST,一个是HEAD(为测试,实际中无任何作用,只是测试是否有效)。


以下代码简单的模拟了一下请求:

package http.business;

public interface IHttp {

}




package http.business;

public interface ISafe {

	public void TestHttpCommandSafe(String type,String url,String ip);

}




package http.business;


public interface IControl {

	public void commandList(Command command);

	public void checkListNull();
}




package http.business;

public class Command {

	private String type;
	private String url;
	private String ip;
	private Command command;


	public Command getCommandRecord(){
		Command command=new Command();
		command.setIp(this.getIp());
		command.setType(this.getType());
		command.setUrl(this.getUrl());
		return command;
	}


	public void setCommandRecord(String type,String url,String ip){
		this.type=type;
		this.ip=ip;
		this.url=url;
	}

	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getIp() {
		return ip;
	}
	public void setIp(String ip) {
		this.ip = ip;
	}



}





package http.business;

import java.util.ArrayList;
import java.util.List;

public class Control implements IControl {


	private static List<Command> commandRecord=new ArrayList<Command>();



	public void commandList(Command command){

		commandRecord.add(command);

		checkListNull();


	}


	 public void checkListNull(){

			if(commandRecord.size()>=1){
			for(int i=0;i<commandRecord.size();i++){


				IHttp http=new Http(commandRecord.get(i).getType(), commandRecord.get(i).getUrl(), commandRecord.get(i).getIp()); 


				Command command=new Command();
				command=commandRecord.get(i);
				this.commandRecord.remove(command);

				}
			}

		}

	}





package http.business;


public class Http implements IHttp{

	private String url;
	private String ip;

	private String type;


	private static String flag="YES";


	private String lineFlag;


	public static String sFlag="1";




	public Http(String type,String b,String c){
		if(flag.equals("YES")) { 
			flag="NO";
			this.url=b;
			this.ip=c;
			this.type=type;
			this.lineFlag="NO";
			sFlag="1";
				if(type.equals("GET")){
						this.Get(this.url,this.ip);
					}else if(type.equals("POST")){
						this.Post(this.url,this.ip);
						}else{
						this.Head(this.url,this.ip);
					}	
			}else{

				flag="YES";
				sFlag="1";

				IControl control=new Control();
				Command command=new Command();

				command.setCommandRecord(type, b, c);
				control.commandList(command.getCommandRecord());
			}
	}

	public Http(){

	}



	public void http(String type,String b,String c){
		IHttp http=new Http(type,b,c);

	}


	private void Get(String b,String c){

		System.out.println("GET....OK!");
		System.out.println("请求的URL为:"+b);
		System.out.println("您的IP为:"+c);
	}


	private void Post(String b,String c){
		System.out.println("POST....OK!");
		System.out.println("请求的URL为:"+b);
		System.out.println("您的IP为:"+c);
	}



	private void Head(String b,String c){
		System.out.println("HEAD.....OK!");
		System.out.println("请求的URL为:"+b);
		System.out.println("您的IP为:"+c);
	}





	public static void close(){
		flag="YES";
	}



	public static void LookState(){
		if(sFlag.equals("2")){
			System.out.println("您的请求列队中");
		}else if(sFlag.equals("1")){
		System.out.println("您的请求正在执行");
		}else if(sFlag.equals("3")){
			System.out.println("您的请求非法");
		}

	}

}





package http.business;

public  class Safe implements ISafe{

	public Safe(String type,String url,String ip){

		this.TestHttpCommandSafe(type,url,ip);
	}


	public void TestHttpCommandSafe(String type,String url,String ip){


		CheckHttpCommandASafe check=new CheckHttpCommandSafe();
		check.checkHttpCommandSafe(type, url, ip);
	}
}




package http.business;

public abstract class CheckHttpCommandASafe {

	final void checkHttpCommandSafe(String type,String url,String ip){

		String ret1=GETandHTTPandPostSafe(type,url,ip);

		String ret2=checkRequestIpSafe(ip);

		if(ret1.equals("Success")&&ret2.equals("Success")){IHttp http=new Http(type,url,ip);}

	}

		abstract String GETandHTTPandPostSafe(String type,String url,String ip);

		abstract String checkRequestIpSafe(String url);
}





package http.business;

public class CheckHttpCommandSafe extends CheckHttpCommandASafe{



	public String GETandHTTPandPostSafe(String type,String url,String ip) {

		if(type.equals("GET")||type.equals("POST")||type.equals("HEAD")){
				return "Success";
				}else{
				System.out.println("您的请求指令有错,未被执行。请确定您输入的为:GET/POST/HEAD?");
				Http.sFlag="3";
				return "Error";
			}

	}






	public String checkRequestIpSafe(String ip) {

		String iIp[]={"000.000.000.000","255.255.255.255","888.888.888.888","444.444.444.444"};
		for(int j=0;j<iIp.length;j++){
			if(ip.equals(iIp[j])){
				    Http.sFlag="3";
					System.out.println("您的IP段非法,服务器拒绝您的请求。");
					return "Error";
				}
			}
			return "Success";

	}

}





package http.test;


import http.business.Http;
import http.business.ISafe;
import http.business.Safe;

public class HttpGetTest {

	public void print(){
		System.out.println("-----------------------------------------------------------");
	}

	public static void main(String[] args) {

		HttpGetTest h=new HttpGetTest();

		ISafe get=new Safe("XXX","www.xxx.com","xxx.xxx.xxx.xxx");
		Http.LookState();
		h.print();

		ISafe get1=new Safe("YYY","www.xxx.com","xxx.xxx.xxx.xxx");
		Http.LookState();
		h.print();

		ISafe get11=new Safe("GET","11111","111111");
		Http.LookState();
		h.print();

		ISafe get22=new Safe("GET","22222","222222");
		Http.LookState();
		h.print();

		ISafe get33=new Safe("GET","3333333","333333");
		Http.LookState();
		h.print();

		ISafe get44=new Safe("GET","444444","444444");
		Http.LookState();
		h.print();

		ISafe get55=new Safe("GET","5555555","555555");
		Http.LookState();
		h.print();

		ISafe get10=new Safe("GET","www.2xxx.com","222.xxx.xxx.xxx");
		Http.LookState();
		h.print();

		ISafe get2=new Safe("GET","www.2xxx.com","222.xxx.xxx.xxx");
		Http.LookState();
		//Http.close(); 
		h.print();

		ISafe get3=new Safe("POST","www.3xxx.com","333.xxx.xxx.xxx");
		Http.LookState();
		Http.close();
		h.print();

		ISafe get4=new Safe("GET","www.xxx.com","000.000.000.000");
		Http.LookState();
		h.print();

		ISafe get5=new Safe("GET","www.xxx.com","188.888.888.888");
		Http.LookState();
		Http.close();
		h.print();

		ISafe get6=new Safe("GET","www.xxx.com","000.000.000.000");
		Http.LookState();
		Http.close();
		h.print();
	}

}





运行结果:




您的请求指令有错,未被执行。请确定您输入的为:GET/POST/HEAD?


您的请求非法


-----------------------------------------------------------


您的请求指令有错,未被执行。请确定您输入的为:GET/POST/HEAD?


您的请求非法


-----------------------------------------------------------


GET....OK!


请求的URL为:11111


您的IP为:111111


您的请求正在执行


-----------------------------------------------------------


GET....OK!


请求的URL为:22222


您的IP为:222222


您的请求正在执行


-----------------------------------------------------------


GET....OK!


请求的URL为:3333333


您的IP为:333333


您的请求正在执行


-----------------------------------------------------------


GET....OK!


请求的URL为:444444


您的IP为:444444


您的请求正在执行


-----------------------------------------------------------


GET....OK!


请求的URL为:5555555


您的IP为:555555


您的请求正在执行


-----------------------------------------------------------


GET....OK!


请求的URL为:www.2xxx.com


您的IP为:222.xxx.xxx.xxx


您的请求正在执行


-----------------------------------------------------------


GET....OK!


请求的URL为:www.2xxx.com


您的IP为:222.xxx.xxx.xxx


您的请求正在执行


-----------------------------------------------------------


POST....OK!


请求的URL为:www.3xxx.com


您的IP为:333.xxx.xxx.xxx


您的请求正在执行


-----------------------------------------------------------


您的IP段非法,服务器拒绝您的请求。


您的请求非法


-----------------------------------------------------------


GET....OK!


请求的URL为:www.xxx.com


您的IP为:188.888.888.888


您的请求正在执行


-----------------------------------------------------------


您的IP段非法,服务器拒绝您的请求。


您的请求非法


-----------------------------------------------------------