HTTP-Version Status-Code Reason-Phrase CRLF
其中,HTTP-Version表示服务器HTTP协议的版本;Status-Code表示服务器发回的响应状态代码;Reason-Phrase表示状态代码的文本描述。
状态代码有三位数字组成,第一个数字定义了响应的类别,且有五种可能取值:
1xx:指示信息--表示请求已接收,继续处理
2xx:成功--表示请求已被成功接收、理解、接受
3xx:重定向--要完成请求必须进行更进一步的操作
4xx:客户端错误--请求有语法错误或请求无法实现
5xx:服务器端错误--服务器未能实现合法的请求
常见状态代码、状态描述、说明:
200 OK //客户端请求成功
400 Bad Request //客户端请求有语法错误,不能被服务器所理解
401 Unauthorized //请求未经授权,这个状态代码必须和WWW-Authenticate报头域一起使用
403 Forbidden //服务器收到请求,但是拒绝提供服务
404 Not Found //请求资源不存在,eg:输入了错误的URL
500 Internal Server Error //服务器发生不可预期的错误
503 Server Unavailable //服务器当前不能处理客户端的请求,一段时间后可能恢复正常
其中,HTTP-Version表示服务器HTTP协议的版本;Status-Code表示服务器发回的响应状态代码;Reason-Phrase表示状态代码的文本描述。
状态代码有三位数字组成,第一个数字定义了响应的类别,且有五种可能取值:
1xx:指示信息--表示请求已接收,继续处理
2xx:成功--表示请求已被成功接收、理解、接受
3xx:重定向--要完成请求必须进行更进一步的操作
4xx:客户端错误--请求有语法错误或请求无法实现
5xx:服务器端错误--服务器未能实现合法的请求
常见状态代码、状态描述、说明:
200 OK //客户端请求成功
400 Bad Request //客户端请求有语法错误,不能被服务器所理解
401 Unauthorized //请求未经授权,这个状态代码必须和WWW-Authenticate报头域一起使用
403 Forbidden //服务器收到请求,但是拒绝提供服务
404 Not Found //请求资源不存在,eg:输入了错误的URL
500 Internal Server Error //服务器发生不可预期的错误
503 Server Unavailable //服务器当前不能处理客户端的请求,一段时间后可能恢复正常
eg:HTTP/1.1 200 OK (CRLF)
二、HTTPS
HTTPS(Hypertext Transfer Protocol over Secure Socket Layer,基于SSL的HTTP协议)使用了HTTP协议,但HTTPS使用不同于HTTP协议的默认端口及一个加密、身份验证层(HTTP与TCP之间)。这个协议的最初研发由网景公司进行,提供了身份验证与加密通信方法,现在它被广泛用于互联网上安全敏感的通信。
客户端在使用HTTPS方式与Web服务器通信时有以下几个步骤,如图所示。
(1)客户使用https的URL访问Web服务器,要求与Web服务器建立SSL连接。
(2)Web服务器收到客户端请求后,会将网站的证书信息(证书中包含公钥)传送一份给客户端。
(3)客户端的浏览器与Web服务器开始协商SSL连接的安全等级,也就是信息加密的等级。
(4)客户端的浏览器根据双方同意的安全等级,建立会话密钥,然后利用网站的公钥将会话密钥加密,并传送给网站。
(5)Web服务器利用自己的私钥解密出会话密钥。
(6)Web服务器利用会话密钥加密与客户端之间的通信。
工具类为HttpsUtil.java
View Code
public class HttpsUtil { static TrustManager[] xtmArray = new MytmArray[] { new MytmArray() };// 创建信任规则列表 private final static int CONNENT_TIMEOUT = 15000; private final static int READ_TIMEOUT = 15000; static HostnameVerifier DO_NOT_VERIFY = new HostnameVerifier() { public boolean verify(String hostname, SSLSession session) { return true; } }; /** * 信任所有主机-对于任何证书都不做检查 Create a trust manager that does not validate * certificate chains, Android 采用X509的证书信息机制,Install the all-trusting trust * manager */ private static void trustAllHosts() { try { SSLContext sc = SSLContext.getInstance("TLS"); sc.init(null, xtmArray, new java.security.SecureRandom()); HttpsURLConnection .setDefaultSSLSocketFactory(sc.getSocketFactory()); // 不进行主机名确认,对所有主机 HttpsURLConnection.setDefaultHostnameVerifier(DO_NOT_VERIFY); } catch (Exception e) { e.printStackTrace(); } } // /** // * https get方法,返回值是https请求,服务端返回的数据string类型,数据进行xml解析 // * */ // public static String HttpsGet(String httpsurl) { // return HttpsPost(httpsurl, null); // // } /** * https post方法,返回值是https请求,服务端返回的数据string类型,数据进行xml解析 * */ public static String HttpsPost(String httpsurl, String data) { String result = null; HttpURLConnection http = null; URL url; try { url = new URL(httpsurl); // 判断是http请求还是https请求 if (url.getProtocol().toLowerCase().equals("https")) { trustAllHosts(); http = (HttpsURLConnection) url.openConnection(); ((HttpsURLConnection) http).setHostnameVerifier(DO_NOT_VERIFY);// 不进行主机名确认 } else { http = (HttpURLConnection) url.openConnection(); } http.setConnectTimeout(CONNENT_TIMEOUT);// 设置超时时间 http.setReadTimeout(READ_TIMEOUT); if (data == null) { http.setRequestMethod("GET");// 设置请求类型 http.setDoInput(true); // http.setRequestProperty("Content-Type", "text/xml"); if (AppSession.mCookieStore != null) http.setRequestProperty("Cookie", AppSession.mCookieStore); } else { http.setRequestMethod("POST");// 设置请求类型为post http.setDoInput(true); http.setDoOutput(true); // http.setRequestProperty("Content-Type", "text/xml"); if (AppSession.mCookieStore != null && AppSession.mCookieStore.trim().length() > 0) http.setRequestProperty("Cookie", AppSession.mCookieStore); DataOutputStream out = new DataOutputStream( http.getOutputStream()); out.writeBytes(data); out.flush(); out.close(); } // 设置http返回状态200(ok)还是403 AppSession.httpsResponseCode = http.getResponseCode(); BufferedReader in = null; if (AppSession.httpsResponseCode == 200) { getCookie(http); in = new BufferedReader(new InputStreamReader( http.getInputStream())); } else in = new BufferedReader(new InputStreamReader( http.getErrorStream())); String temp = in.readLine(); while (temp != null) { if (result != null) result += temp; else result = temp; temp = in.readLine(); } in.close(); http.disconnect(); } catch (Exception e) { e.printStackTrace(); } return result; } /** * 得到cookie * */ private static void getCookie(HttpURLConnection http) { String cookieVal = null; String key = null; AppSession.mCookieStore = ""; for (int i = 1; (key = http.getHeaderFieldKey(i)) != null; i++) { if (key.equalsIgnoreCase("set-cookie")) { cookieVal = http.getHeaderField(i); cookieVal = cookieVal.substring(0, cookieVal.indexOf(";")); AppSession.mCookieStore = AppSession.mCookieStore + cookieVal + ";"; } } } }