Httpclint 4.x 的用法

来源:互联网 发布:湖南第五届网络文化节 编辑:程序博客网 时间:2024/05/23 02:02

Httpclint 4.x 的用法 直接上代码 

package service.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;  
import org.apache.http.client.methods.CloseableHttpResponse;  
import org.apache.http.client.methods.HttpGet;  
import org.apache.http.client.methods.HttpPost;  
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;  
import org.apache.http.impl.client.HttpClients;  
import org.apache.http.util.EntityUtils;  
import org.apache.http.NameValuePair;  
  
@SuppressWarnings("unused")
public class HttpUtils {  

    //定义httpClient 同时设置自动保存cookie信息
    static CookieStore cookieStore = new BasicCookieStore();  
    private static CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    private static HttpClientContext context = new HttpClientContext();    
    
    private HttpUtils() {             
    }   
      

    //get功能
    public static String sendGet(String url) {    
        CloseableHttpResponse response = null;    
        String content = null;    
        try {    
            HttpGet get = new HttpGet(url);    

            get.setHead("";"");//设置头信息

            //发送Get请求
            response = httpClient.execute(get, context);  
            HttpEntity entity = response.getEntity();    
            content = EntityUtils.toString(entity);    
            return content;    
        } catch (Exception e) {    
            e.printStackTrace();    
            if (response != null) {    
                try {    
                    response.close();    
                } catch (IOException e1) {    
                    e1.printStackTrace();    
                }    
            }    
        }    
        return content;    
    }    
    

    //post方法
    public static String sendPost(String url, List<NameValuePair> nvps) {    
        CloseableHttpResponse response = null;    
        String content = null;    
        try {    
            HttpPost post = new HttpPost(url);    
            if (nvps != null) {    

                //设置post data
                post.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));    
            }      

            //发送Post请求
            response = httpClient.execute(post, context);    
            HttpEntity entity = response.getEntity();    
            content = EntityUtils.toString(entity);  
            return content;    
        } catch (Exception e) {    
            e.printStackTrace();    
        } finally {    
            if (response != null) {    
                try {    
                    response.close();    
                } catch (IOException e) {    
                    e.printStackTrace();    
                }    
            }    
        }    
        return content;    
    }     
}