您可以执行以下操作:
HttpClient client = getNewHttpClient();
// Create a local instance of cookie store
CookieStore cookieStore = new BasicCookieStore();
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
try {
request = new HttpPost(url);
// request.addHeader("Accept-Encoding", "gzip");
} catch (Exception e) {
e.printStackTrace();
}
if (postParameters != null && postParameters.isEmpty() == false) {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
postParameters.size());
String k, v;
Iterator<String> itKeys = postParameters.keySet().iterator();
while (itKeys.hasNext()) {
k = itKeys.next();
v = postParameters.get(k);
nameValuePairs.add(new BasicNameValuePair(k, v));
}
UrlEncodedFormEntity urlEntity = new UrlEncodedFormEntity(
nameValuePairs);
request.setEntity(urlEntity);
}
try {
Response = client.execute(request, localContext);
HttpEntity entity = Response.getEntity();
int statusCode = Response.getStatusLine().getStatusCode();
Log.i(TAG, "" + statusCode);
Log.i(TAG, "------------------------------------------------");
if (entity != null) {
Log.i(TAG,
"Response content length:" + entity.getContentLength());
}
List<Cookie> cookies = cookieStore.getCookies();
for (int i = 0; i < cookies.size(); i++) {
Log.i(TAG, "Local cookie: " + cookies.get(i));
}
try {
InputStream in = (InputStream) entity.getContent();
// Header contentEncoding =
// Response.getFirstHeader("Content-Encoding");
/*
* if (contentEncoding != null &&
* contentEncoding.getValue().equalsIgnoreCase("gzip")) { in =
* new GZIPInputStream(in); }
*/
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
StringBuilder str = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
Log.i(TAG, "" + str.append(line + "\n"));
}
in.close();
response = str.toString();
Log.i(TAG, "response" + response);
} catch (IllegalStateException exc) {
exc.printStackTrace();
}
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + response);
} finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
// client.getConnectionManager().shutdown();
}
return response;
enter code here