通常来说一个HTTP请求报文由请求行、请求报头、空行、和请求数据4个部分组成。
请求行
请求行由请求方法,URL字段和HTTP协议的版本组成,格式如下:
Method Request-URI HTTP-Version CRLF
其中 Method表示请求方法;Request-URI是一个统一资源标识符;HTTP-Version表示请求的HTTP协议版本;CRLF表示回车和换行(除了作为结尾的CRLF外,不允许出现单独的CR或LF字符)。
HTTP请求方法有8种,分别是GET、POST、DELETE、PUT、HEAD、TRACE、CONNECT 、OPTIONS。其中PUT、DELETE、POST、GET分别对应着增删改查,对于移动开发最常用的就是POST和GET了。
1.GET:请求获取Request-URI所标识的资源
2.POST:在Request-URI所标识的资源后附加新的数据
3.HEAD:请求获取由Request-URI所标识的资源的响应消息报头
4.PUT: 请求服务器存储一个资源,并用Request-URI作为其标识
5.DELETE :请求服务器删除Request-URI所标识的资源
6.TRACE : 请求服务器回送收到的请求信息,主要用于测试或诊断
7.CONNECT: HTTP/1.1协议中预留给能够将连接改为管道方式的代理服务器。
8.OPTIONS :请求查询服务器的性能,或者查询与资源相关的选项和需求
举个例子:
GET http://www.jianshu.com/users/351c7ec643b5/collections_and_notebooks?slug=351c7ec643b5 HTTP/1.1
请求报头
在请求行之后会有0个或者多个请求报头,每个请求报头都包含一个名字和一个值,它们之间用“:”分割。请求头部会以一个空行,发送回车符和换行符,通知服务器以下不会有请求头。
请求数据
请求数据不在GET方法中使用,而是在POST方法中使用。POST方法适用于需要客户填写表单的场合,与请求数据相关的最常用的请求头是Content-Type和Content-Length。
那么Request是怎么把这些信息用java语言表现出来的呢?来看一看Request类,它是一个抽象类,我们通常使用它的子类StringRequest或者JsonRequset来进行网络访问。举个例子:
StringRequest mStringRequest = new StringRequest(Request.Method.POST,"https://xxx.xxxxx.com/",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("volley", response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("volley", error.getMessage(), error);
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("type","xxxx");
map.put("op_type","x");
map.put("op_role","x");
return map;
}
};
在StringRequest的构造方法里面将会调用父类的构造方法:
super(method, url, errorListener);
将参数传递过去。来看一看Request类定义接收参数的成员变量:
/**
* Request method of this request. Currently supports GET, POST, PUT, DELETE, HEAD, OPTIONS,
* TRACE, and PATCH.
*/
private final int mMethod;
/** URL of this request. */
private final String mUrl;
/** Listener interface for errors. */
private final Response.ErrorListener mErrorListener;
同时通过复写getParams()方法来进行Post参数传递
/**
* Returns a Map of parameters to be used for a POST or PUT request. Can throw
* {@link AuthFailureError} as authentication may be required to provide these values.
*
* <p>Note that you can directly override {@link #getBody()} for custom data.</p>
*
* @throws AuthFailureError in the event of auth failure
*/
protected Map<String, String> getParams() throws AuthFailureError {
return null;
}
其他一些在看源码时使用的成员变量:
/** Sequence number of this request, used to enforce FIFO ordering. Sequence=序列号 */
private Integer mSequence;
/** The request queue this request is associated with. */
private RequestQueue mRequestQueue;
/** Whether or not responses to this request should be cached. */
private boolean mShouldCache = true;
/** Whether or not this request has been canceled. */
private boolean mCanceled = false;
/** Whether or not a response has been delivered for this request yet. */
private boolean mResponseDelivered = false;
/** Whether the request should be retried in the event of an HTTP 5xx (server) error. */
private boolean mShouldRetryServerErrors = false;
/** The retry policy for this request. 接口 用于返回超时时间和重试次数 */
private RetryPolicy mRetryPolicy;
/**
* When a request can be retrieved from cache but must be refreshed from
* the network, the cache entry will be stored here so that in the event of
* a "Not Modified" response, we can be sure it hasn't been evicted from cache.
*/
private Cache.Entry mCacheEntry = null;
/** Listener that will be notified when a response has been delivered. */
private NetworkRequestCompleteListener mRequestCompleteListener;
/**
* Returns the content type of the POST or PUT body.
*/
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=" + getParamsEncoding();
}
大概就是这些了。。
网友评论