Httpclint 4.x 的用法

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;    
    }     
}  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值