如何添加参数以及如何获取字符串? (java,httpclient 4.X)
代码 Http get 请求:
HttpClient httpclient = new DefaultHttpClient();
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);
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) {
}
}
如何获取响应 STRING ?
我需要创建请求 POST、添加参数和自动重定向。
Code Http get request:
HttpClient httpclient = new DefaultHttpClient();
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);
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能有一种更快的方法来执行此操作,但这将使您得到字符串形式的响应:
要创建发布请求,请使用:
或 HttpPost: http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
HttpClient 应该为您处理自动重定向
There might be a quicker way to do this but this will get you the response as a string:
To create a post request use:
Or HttpPost: http://www.androidsnippets.com/executing-a-http-post-request-with-httpclient
HttpClient should handle the automatic redirects for you