简单通信例子
//json格式的请求报文
JSONObject params = new JSONObject()
.fluentPut("msgID","TA0040001000220191009015734015734")
.fluentPut("useID","A00400010002");
//发送请求消息
String reqJson = JSON.toJSONString(params);
//httpClient通信
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(payurl);//payurl 通信的网址
StringEntity entityParams = new StringEntity(reqJson, "UTF-8");//reqJson 请求json格式的报文
httpPost.setEntity(entityParams);
httpPost.setHeader("Content-Type", "application/json");//报文内容数据格式,此为json
httpPost.setHeader("Authorization", "XXOO");//授权验证的信息
HttpResponse httpResponse = httpClient.execute(httpPost);
header信息获取
通信需求有时需要验证html header中的授权信息,或者游戏特殊的签名信息放在header中。此时需要读取出对应的header信息。
可以使用
getFirstHeader("name").getValue();
获取对应头部的信息。
例子:
httpPost.setHeader("Authorization", "XXOO");//签名
System.out.println("request header :" + httpPost.getFirstHeader("Authorization"));
System.out.println("request header name:" + httpPost.getFirstHeader("Authorization").getName());
System.out.println("request header value:" + httpPost.getFirstHeader("Authorization").getValue());
HttpResponse httpResponse = httpClient.execute(httpPost);
结果:
返回报文也可如此操作:
HttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println("response header:" + httpResponse.getFirstHeader("Authorization"));
附:分析HttpMessage
HttpMessage提供出的接口有:
/**
* Returns the protocol version this message is compatible with.
*/
ProtocolVersion getProtocolVersion();
/**
* Checks if a certain header is present in this message. Header values are
* ignored.
*
* @param name the header name to check for.
* @return true if at least one header with this name is present.
*/
boolean containsHeader(String name);
/**
* Returns all the headers with a specified name of this message. Header values
* are ignored. Headers are orderd in the sequence they will be sent over a
* connection.
*
* @param name the name of the headers to return.
* @return the headers whose name property equals {@code name}.
*/
Header[] getHeaders(String name);
/**
* Returns the first header with a specified name of this message. Header
* values are ignored. If there is more than one matching header in the
* message the first element of {@link #getHeaders(String)} is returned.
* If there is no matching header in the message {@code null} is
* returned.
*
* @param name the name of the header to return.
* @return the first header whose name property equals {@code name}
* or {@code null} if no such header could be found.
*/
Header getFirstHeader(String name);
/**
* Returns the last header with a specified name of this message. Header values
* are ignored. If there is more than one matching header in the message the
* last element of {@link #getHeaders(String)} is returned. If there is no
* matching header in the message {@code null} is returned.
*
* @param name the name of the header to return.
* @return the last header whose name property equals {@code name}.
* or {@code null} if no such header could be found.
*/
Header getLastHeader(String name);
/**
* Returns all the headers of this message. Headers are orderd in the sequence
* they will be sent over a connection.
*
* @return all the headers of this message
*/
Header[] getAllHeaders();
/**
* Adds a header to this message. The header will be appended to the end of
* the list.
*
* @param header the header to append.
*/
void addHeader(Header header);
/**
* Adds a header to this message. The header will be appended to the end of
* the list.
*
* @param name the name of the header.
* @param value the value of the header.
*/
void addHeader(String name, String value);
/**
* Overwrites the first header with the same name. The new header will be appended to
* the end of the list, if no header with the given name can be found.
*
* @param header the header to set.
*/
void setHeader(Header header);
/**
* Overwrites the first header with the same name. The new header will be appended to
* the end of the list, if no header with the given name can be found.
*
* @param name the name of the header.
* @param value the value of the header.
*/
void setHeader(String name, String value);
/**
* Overwrites all the headers in the message.
*
* @param headers the array of headers to set.
*/
void setHeaders(Header[] headers);
/**
* Removes a header from this message.
*
* @param header the header to remove.
*/
void removeHeader(Header header);
/**
* Removes all headers with a certain name from this message.
*
* @param name The name of the headers to remove.
*/
void removeHeaders(String name);
/**
* Returns an iterator of all the headers.
*
* @return Iterator that returns Header objects in the sequence they are
* sent over a connection.
*/
HeaderIterator headerIterator();
/**
* Returns an iterator of the headers with a given name.
*
* @param name the name of the headers over which to iterate, or
* {@code null} for all headers
*
* @return Iterator that returns Header objects with the argument name
* in the sequence they are sent over a connection.
*/
HeaderIterator headerIterator(String name);
/**
* Returns the parameters effective for this message as set by
* {@link #setParams(HttpParams)}.
*
* @deprecated (4.3) use configuration classes provided 'org.apache.http.config'
* and 'org.apache.http.client.config'
*/
@Deprecated
HttpParams getParams();
/**
* Provides parameters to be used for the processing of this message.
* @param params the parameters
*
* @deprecated (4.3) use configuration classes provided 'org.apache.http.config'
* and 'org.apache.http.client.config'
*/
@Deprecated
void setParams(HttpParams params);
可见获取header信息的函数有:
Header[] getHeaders(String name); 获取匹配name的header数组返回结果
Header getFirstHeader(String name); 获取匹配的第一个name的header
Header getLastHeader(String name); 获取匹配的最后一个name的header
Header[] getAllHeaders(); 获取全部的header
确定某个header name唯一的情况,感觉getFirstHeader比较好用。