Code Http get request:
HttpClient httpclient = new DefaultHttpClient();
CookieStore cookieStore = new Bas开发者_如何学运维icCookieStore();
// Create local HTTP context
HttpContext localContext = new BasicHttpContext();
// Bind custom cookie store to the local context
localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
HttpGet first = new HttpGet("http://vk.com");
HttpResponse response = httpclient.execute(first, localContext);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
int l;
byte[] tmp = new byte[2048];
while ((l = instream.read(tmp)) != -1) {
}
}
How get responce STRING ?
And I need create request POST, add params and auto redirect.
There might be a quicker way to do this but this will get you the response as a string:
InputStream = response.getEntity().getContent();
StringBuilder sb = new StringBuilder();
BufferedReader r = new BufferedReader(new InputStreamReader(in));
for (String line = r.readLine(); line != null; line = r.readLine()) {
sb.append(line);
}
in.close();
String s = sb.toString();
To create a post request use:
PostMethod post = new PostMethod("http://url.com");
post.addParameter("param1name", "param1value");
Or HttpPost: http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
HttpClient should handle the automatic redirects for you
精彩评论