首页 > 代码库 > Apache HttpClient访问网络工具类
Apache HttpClient访问网络工具类
1
package com.ztravel.utils; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class HttpClientUtils { public static String sendHttpclientPost(String path, Map<String, String> map, String encode) { // TODO Auto-generated method stub // 填充表单的内容 List<NameValuePair> pairs = new ArrayList<NameValuePair>(); // Key必须与action接收params的key一致 if (map != null && !map.isEmpty()) { // 一个entry代表一个键值对,.getKey()获取键,getValue()获取值 for (Map.Entry<String, String> entry : map.entrySet()) { pairs.add(new BasicNameValuePair(entry.getKey(), entry .getValue())); } } try { // 添加form表单的内容,和网页的jsp一样 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pairs, encode); // 使用post提交数据 HttpPost httpPost = new HttpPost(path); httpPost.setEntity(entity); HttpClient httpClient = new DefaultHttpClient(); // HttpResponse获得服务器响应的所有消息 HttpResponse response = httpClient.execute(httpPost); // 判断连接成功 if (response.getStatusLine().getStatusCode() == 200) { // HttpEntity获得服务器响应回来的消息体(不包括HTTP HEAD) HttpEntity httpEntity = response.getEntity(); // 自动分辨响应的内容的编码 String defaultCharset = EntityUtils .getContentCharSet(httpEntity); // EntityUtils.toString将获得的消息体转换成String return EntityUtils.toString(httpEntity, defaultCharset); } } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // 因为结果经常调用string的函数,所以没有返回null return ""; } public static String sendHttpclientGet(HttpClient httpClient, String path) { try {// 利用HTTPGET 向服务器发起请求 // 如果URL连接过程中302重定向,则会自动跳转到新的页面 HttpGet get = new HttpGet(path); // HttpResponse获得服务器响应的所有消息 HttpResponse response = httpClient.execute(get); // HttpEntity获得服务器响应回来的消息体(不包括HTTP HEAD) HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) {// 获取消息体的内容 String defaultCharset = EntityUtils .getContentCharSet(httpEntity); // EntityUtils.toString将获得的消息体转换成String return EntityUtils.toString(httpEntity, defaultCharset); } } catch (Exception e) { } return "";// 返回"",但不是空值 } /** * 从网络获取图片字节数组 * @param path * @return */ public static byte[] getByteArrayByUrl(String path) { byte[] data = http://www.mamicode.com/null; try {// 利用HTTPGET 向服务器发起请求 // 如果URL连接过程中302重定向,则会自动跳转到新的页面 HttpGet get = new HttpGet(path); HttpClient httpClient=new DefaultHttpClient();; // HttpResponse获得服务器响应的所有消息 HttpResponse response = httpClient.execute(get); // HttpEntity获得服务器响应回来的消息体(不包括HTTP HEAD) HttpEntity httpEntity = response.getEntity(); if (httpEntity != null) {// 获取消息体的内容 data =http://www.mamicode.com/ EntityUtils.toByteArray(httpEntity); } } catch (Exception e) { } return data;// 返回"",但不是空值 } }
Done
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。