本文:目的和前一篇一样,唯一区别是本文用apache标准接口进行http的post提交数据,而前一篇是用 java标准接口实现。
---------------------------------------------------------------------------------------------------------------------
重点:
1. public static String sendHttpClientPost(String path, Map<String, String> params, String encode)
内部用apache接口实现http的post提交数据。
2. public static String changeInputStream(InputStream inputStream, String encode)
实现 将一个输入流转变成一个字符串,用指定编码。
---------------------------------------------------------------------------------------------------------------------
注意点:
一.用apache标准接口实现http 的Post提交数据的 关键步骤?
1. 将参数封装到表单请求体中。
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, encode);
2. 使用post方式提交数据
HttpPost httpPost = new HttpPost(path);
httpPost.setEntity(entity);
3. 执行post请求,并获取服务器端的响应HttpResponse
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse httpResponse = client.execute(httpPost);
4. 获取服务器端返回的状态码和输入流,将输入流转换成字符串
if (httpResponse.getStatusLine().getStatusCode() == 200) {
InputStream inputStream = httpResponse.getEntity().getContent();
return changeInputStream(inputStream, encode);
}
二. 编写次程序,要将http协议包添加到 build path , http 协议包在我的资源中。
------------------------------------------------------------------------------------------------------------------------
程序运行结果图:
1