apache httpclient实现和html解析

黄斯俊 发布于 2012/11/14 19:08
阅读 1K+
收藏 0

@红薯 你好,想跟你请教个问题:你能帮我实现下今天我提问的问题吗?就是apache httpclient实现模拟登陆和html解析。你推荐的我都看过了,但那些只是一部分代码,很难看懂!能帮我写下或者给个相近的项目行不?

加载中
0
红薯
红薯
抱歉,你还是硬着头皮看吧
0
上郡刀客
上郡刀客
public static byte[] readDataByHttpPost(String server, NameValuePair... args) throws Exception {
		HttpClient httpClient = new DefaultHttpClient();

		try {
			HttpPost httpPost = new HttpPost(server);
			List<NameValuePair> params = Arrays.asList(args);

			httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));

			HttpResponse response = httpClient.execute(httpPost);

			byte[] data = null;

			if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = response.getEntity();

				InputStream is = entity.getContent();

				data = new byte[(int) entity.getContentLength()];
				int read = 0;

				while(read < data.length - 1)
					read += is.read(data, read, is.available());

				is.close();
			}

			return data;
		}finally {
			httpClient.getConnectionManager().shutdown();
		}
	}
0
上郡刀客
上郡刀客
这个是以前通过http下文件用的
0
上郡刀客
上郡刀客
httpclient存在并发问题 一般要大量数据请求的话用Asynch HttpClient
返回顶部
顶部