没事干抄协议-HTTP

一、简介

       搜下一大堆, 百度百科 所有东西都说完了。

二、内容

       既然是协议那么就有头和体,不论请求还是应答。平时搭建web的时候不关注头,那时因为默认替你搞了其实接头暗号才是根本。

      1.通过firebug直观了解下协议

没事干抄协议-HTTP_第1张图片

      2.最简单的页面

<body>
    <%=request.getParameter("test")%>
    8888端口
<body>	

     3.通过HttpURLConnection建立模拟连接

try {
			URL url=new URL("http://127.0.0.1:8888/WebRoot/servier.jsp?test=123");
			HttpURLConnection hc=(HttpURLConnection) url.openConnection();
           
	       hc.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:29.0) Gecko/20100101 Firefox/29.0");
	       hc.addRequestProperty("test","456");	
			//请求返回体
			String key=null;
			List<String> values=null;
			
			//请求参数
			Map<String,List<String>> reqMap=hc.getRequestProperties();
			Set<String> reqSet=reqMap.keySet();
			
			for(String s:reqSet){
				key=s;
				values=reqMap.get(key);
				System.out.println(key+": "+values.get(0));
			}
	System.out.println("----------------------------------------");		
			//返回参数
			Map<String,List<String>> respMap=hc.getHeaderFields();
			Set<String> keySet=respMap.keySet();
		
			for(String s:keySet){
				key=s;
				values=respMap.get(key);
				System.out.println(key+": "+values.get(0));
			}
			System.out.println("----------------------------------------");	
			//获取的输出内容
			BufferedReader br = new BufferedReader(new InputStreamReader(hc.getInputStream()));
			String sb=new String();
			while(null!=(sb=br.readLine()))
				System.out.println(sb);
			
			br.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

 结果:

test: 456
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:29.0) Gecko/20100101 Firefox/29.0
----------------------------------------
null: HTTP/1.1 200 OK
Date: Mon, 26 May 2014 08:03:04 GMT
Content-Length: 104
Set-Cookie: JSESSIONID=B6DE4FB9E432289B042ABE21E3017AF6; Path=/WebRoot/; HttpOnly
Content-Type: text/html;charset=GBK
Server: Apache-Coyote/1.1
----------------------------------------

<!doctype html>
<html lang="en">
<head>
	
</head>
<body>
    123
    8888端口
<body>	
</html>

     4.哈哈这里我们通过SOCKET来建立连接

    ps:头结尾是以一个空额 \n来判断的,开始没有郁闷一直连着也没结果 

//http://127.0.0.1:8888/WebRoot/servier.jsp?test=123
		
		try {
			Socket s=new Socket("127.0.0.1", 8888);
		  if(s.isConnected()){	
			OutputStream os= s.getOutputStream();
			String connect="GET /WebRoot/servier.jsp?test=123 HTTP/1.1\r\n"+
                            "Host: 127.0.0.1:8888\r\n"+
                            "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:29.0) Gecko/20100101 Firefox/29.0\r\n"+
                            "Accept: text/html\r\n"+
                            "Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3\r\n"+
                            "Accept-Encoding: gzip, deflate\r\n"+
                            "Connection: keep-alive\r\n"+
                            "\r\n";
			                 
			os.write(connect.getBytes());
			os.flush();
			System.out.println(connect);
			System.out.println("......");
			
			BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
			String sb=new String();
			while(null!=(sb=br.readLine()))
				System.out.println(sb);
			
			br.close();
		  }
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

结果:

GET /WebRoot/servier.jsp?test=123 HTTP/1.1
Host: 127.0.0.1:8888
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:29.0) Gecko/20100101 Firefox/29.0
Accept: text/html
Accept-Language: zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Connection: keep-alive


......
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=75D283F2376CEEDE28F2967549972FA9; Path=/WebRoot/; HttpOnly
Content-Type: text/html;charset=GBK
Content-Length: 104
Date: Mon, 26 May 2014 08:04:53 GMT


<!doctype html>
<html lang="en">
<head>
	
</head>
<body>
    123
    8888端口
<body>	
</html>

 三、直观理解了请求转发和重定向

最直观的的方式通过浏览器的URL改变,这里介绍上面的4

重定向:

1.修改下页面

<% response.sendRedirect("http://127.0.0.1:8888/WebRoot/redirect.html");%>

   2.用火狐来请求 内容如下

    3.用SOCKET来请求

      返回的请求头,并且内容输出在阻塞这等待,因为我们没有做下一步动作

没事干抄协议-HTTP_第2张图片

请求转发:

  4.但改用转发的方式的时候,都可以获得结果,但是socket的感觉等了很久,但是结果还是出来了

此处区别:

  浏览器的地址栏

  http有个特性无状态,一个是重新的请求,一个内部调用了方法参数还是传递的

 

  四、关于访问IP的问题   

     因为你会发现http协议没有看到跟ip相关的,但是是建立在tcp之上的,有时间了再研究研究来

     代理后其实有个参数X-Forwarded-For记录信息的传递,但是这不是一个标准的字段

     这有个获取真实IP的写法

   五、针对上面 关于代理的使用差异

                               1.请求的路径会是全路径的

                               2.请求信息Connection-》Proxy-Connection

                                  这个地方不明白的是不知道代理服务器转发的时候头部这个参数是如何处理的?

                                   3.问题还是上面这个参数在1.1版本的一般是keep alive保持了连接的重用

                                     实验

                                     既然是重用了,那么怎么判断这个请求返回值结束了

你可能感兴趣的:(没事干抄协议-HTTP)