通过header可以读出所有的头信息,包括你的PHPSESSION
我自己项目里的,供参考:
/**
* A HTTP request(POST) with given URL
*
* @param strUrl
* the URL you want to request
* @return
*/
public static String post(String url) throws ClientProtocolException, IOException {
String result = "";
String httpUrl = QuhaoConstant.HTTP_URL + url;
HttpPost request = new HttpPost(httpUrl);
request.setHeader("user-agent", "QuhaoAndroid");
// set session id if exist
if(StringUtils.isNotNull(CommonHTTPRequest.sessionID)){
// request.setHeader("Set-Cookie", CommonHTTPRequest.sessionID);
}
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 10 * 1000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 10 * 1000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpResponse response = httpClient.execute(request);
Header[] headers = response.getAllHeaders();
for(Header h : headers){
//这里你打印h.getName 和 h.getValue
QuhaoLog.d(TAG, h.getName() + "|||" + h.getValue());
QuhaoLog.d(TAG, "decoded: "+URLDecoder.decode(h.getValue(), "UTF-8"));
// h.getValue() : PLAY_SESSION=28b7eb241f5b26e778acf4a825f8deddfa123e47-%005289c60bc929b65bbf675278%3A5289c60bc929b65bbf675278%00%00quhao_username%3A5289c60bc929b65bbf675278%00;Path=/
if(h.getValue().contains("PLAY_SESSION")){
// CommonHTTPRequest.sessionID = h.getValue().split(":")[2];
}
}
QuhaoLog.i(TAG, "get data from server, the status code is : " + response.getStatusLine().getStatusCode());
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
result = EntityUtils.toString(response.getEntity());
QuhaoLog.v(TAG, "get data from server : " + result);
}
return result;
}