HttpClient

来源:互联网 发布:金蝶软件多少钱一套 编辑:程序博客网 时间:2024/06/05 21:15
package com.andy.httpcilent;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
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.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 类描述:
 * 创建人:yekh
 * 创建时间:2017/5/11 14:29
 */
public class HttpClientUtils {
    /**
         * 单例模式
         * 1.构造器私有化
         * 2.创建一个静态的类变量(当前类)
         * 3.提供一个静态方法来返回2中的类变量
         */
        private static HttpClientUtils instance;
        private HttpClient client;
        private HttpClientUtils(){
            client=new DefaultHttpClient();
        }
        public static HttpClientUtils getInstance(){
            //双重检索机制
            if(instance==null){
                synchronized (HttpClientUtils.class){
                    if(instance==null){
                        instance=new HttpClientUtils();
                    }
                }
            }
            return instance;
        }

        /**
         * get请求
         * @param url
         * @return
         */
        public String get(String url){
            //创建httpGet
            HttpGet httpGet=new HttpGet(url);
            return getResult(httpGet);
        }

        /**
         * post请求
         * @param url
         * @param map
         * @return
         */
        public String post(String url, Map<String,String> map){
            //创建HttpPost
            HttpPost post=new HttpPost(url);
            if(map!=null&&map.size()>0){
                addEntity(map,post);
            }
            return getResult(post);
        }

        /**
         * 根据请求拿到返回结果
         * @param request
         * @return
         */
        public String getResult(HttpUriRequest request){
            try {
                HttpResponse response = client.execute(request);
                if(response.getStatusLine().getStatusCode()==200){
                    //得到返回实体类HttpEntity
                    HttpEntity entity = response.getEntity();
                    //EntityUtils转换成字符串
                    String result = EntityUtils.toString(entity);
                    return result;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        /**
         * 将map集合中的数据添加到post的请求实体中
         * @param map
         * @param post
         */
        public void addEntity(Map<String,String> map, HttpPost post){
            List<NameValuePair> list=new ArrayList<NameValuePair>();
            for(String key:map.keySet()){
                BasicNameValuePair nameValue=new BasicNameValuePair(key,map.get(key));
                list.add(nameValue);
            }
            try {
                UrlEncodedFormEntity urlEntity=new UrlEncodedFormEntity(list,"utf-8");
                post.setEntity(urlEntity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
}

0 0
原创粉丝点击