Http协议基础
1 简介
超文本传输协议
2 特点
-
传输明文内容,安全性比较差
-
无状态协议
-
应用层协议,标准化协议版本1.1
3 分析工具
-
HttpWatch
-
Fiddler
4 HTTP请求
-
GET请求
该请求是由Request-URI所标识的资源
-
POST请求
该请求是在Request-RUI所标识的资源后附加新的数据
5 请求头和响应头
-
请求头信息
POST /account/login?ref=toolbar HTTP/1.1 Accept: text/html, application/xhtml+xml, */* X-HttpWatch-RID: 17420-10453 Referer: https://passport.csdn.net/account/login?ref=toolbar Accept-Language: zh-CN User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate Host: passport.csdn.net Content-Length: 121 Connection: Keep-Alive Cache-Control: no-cache
username=chengzhangmsdn&password=dcz1001811215<=LT-183545-Gxc09IufqGIeWTlyfAYNL1xKDgGSdA&execution=e2s1&_eventId=submit |
-
响应头信息
HTTP/1.1 302 Found Server: openresty Date: Sat, 03 Oct 2015 10:09:15 GMT Content-Length: 0 Connection: keep-alive Keep-Alive: timeout=20 Pragma: no-cache Expires: Thu, 01 Jan 1970 00:00:00 GMT Cache-Control: no-cache Cache-Control: no-store Set-Cookie: JSESSIONID=88C6B2CDB8C2F34C4FB57877ED910872.tomcat1; Path=/; HttpOnly Location: http://passport.csdn.net/account/login;jsessionid=88C6B2CDB8C2F34C4FB57877ED910872.tomcat1?ref=toolbar
|
6 HTTP-SESSION
Session是由服务器端产生的,是分配给每一个访问服务器的客户端,是唯一标识的。Session默认是在以文件的形式维护在服务器机器上,session文件中记录着session变量信息。
7 HTTP-COOKIE
Cookie是由客户端产生的,可记录一些键值对的信息。可指定记录值得过期时间和作用域等等。它和session很像。
8 HTTP有状态解决方案
由于http是一个无状态的协议,不能保存用户状态。那么直接面临的一个问题就是如当一个用户发起一个HTTP登陆系统的请求时候,由于HTTP自身特点,服务器无法记录用户登录信息,那么服务器是如何解决这个问题的呢?它们是通过服务器端的session和客户端的cookie配合来解决的。
例如我们通过浏览器发送登录请求到服务器端,那么服务器会产生会话同时也会创建一个session ID并以文件的形式保存起来,当响应客户端的时候将把这个session ID传送给客户端浏览器,浏览器会根据响应头的一些必要信息将这个session ID记录到客户端的cookie中。下次我们在访问这个网站的时候,客户端将会带着这个作用域下面的记录的cookie值再次访问这个网站,那么服务器通过session ID就可以知道是哪一个客户端访问的。
9 Java处理HTTP协议
package com.dcz.http;
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.io.PrintWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.List; import java.util.Map;
public class JavaHttpHandler {
public static void main(String[] args) {
JavaHttpHandler javaHttpHandler = new JavaHttpHandler(); // javaHttpHandler.sendGet("http://www.csdn.net/"); javaHttpHandler.sendPost("https://passport.csdn.net/", "username=chengzhangmsdn&password=dcz1001811215<=LT-183545-Gxc09IufqGIeWTlyfAYNL1xKDgGSdA&execution=e2s1&_eventId=submit"); }
//发送GET请求 public void sendGet(String urlAddress) {
HttpURLConnection urlConnection = null; try { URL url = new URL(urlAddress); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setConnectTimeout(5000); urlConnection.setReadTimeout(5000); urlConnection.setUseCaches(false); urlConnection.connect();
//获取响应内容 BufferedReader bf = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "utf-8")); String line = null; while ((line = bf.readLine()) != null) { System.out.println(line); }
} catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
//发送POST请求 public void sendPost(String urlAddress, String paramValue) {
HttpURLConnection urlConnection = null; try { URL url = new URL(urlAddress); urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setConnectTimeout(5000); urlConnection.setReadTimeout(5000); urlConnection.setUseCaches(false); urlConnection.setDoOutput(true); urlConnection.connect();
OutputStream outputStream = urlConnection.getOutputStream(); PrintWriter pw = new PrintWriter(outputStream); pw.print(paramValue); pw.flush();
Map<String, List<String>> headerMap = urlConnection.getHeaderFields();
//请响应头获取需要设置的Cookie信息 List<String> list = headerMap.get("Set-Cookie");
for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); }
} catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
}
|