http通过post请求调用接口

org.apache.httpcomponents httpclient
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
    </dependency>

api6:
httpurl: xxx
@Value("${api.httpurl}")
private String url;

List listj = new LinkedList<>();

        BasicNameValuePair params3 = new BasicNameValuePair("Search", "");
       // BasicNameValuePair params4 = new BasicNameValuePair("DeviceType", "");
        BasicNameValuePair params5 = new BasicNameValuePair("page", "1");
        BasicNameValuePair params6 = new BasicNameValuePair("pageSize", "15");

import org.apache.http.HttpEntity;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

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

//s
public class httpclient {

public static String doPostByJson(String apiUrl, List list,String token) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    String httpStr = null;
    HttpPost httpPost = new HttpPost(apiUrl);

// List lists = new LinkedList<>();
// BasicNameValuePair param1 = new BasicNameValuePair(“username”, “acrel”);
// BasicNameValuePair param2 = new BasicNameValuePair(“password”, “acrel001”);
// BasicNameValuePair param3 = new BasicNameValuePair(“userid”, “47”);
// list.add(param1);
// list.add(param2);
// list.add(param3);

    try {
        UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
        httpPost.setEntity(entityParam);
        httpPost.setEntity(entityParam);
        // 浏览器表示
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
        // 传输的类型
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        if (token!=null&&token!=""){
            httpPost.addHeader("token", token);
        }

        // 执行请求
        response = httpClient.execute(httpPost);
        // 获得响应的实体对象
        HttpEntity entity = response.getEntity();
        // 使用Apache提供的工具类进行转换成字符串
        httpStr = EntityUtils.toString(entity, "UTF-8");


    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (response != null) {
            try {
                EntityUtils.consume(response.getEntity());
                response.close();
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return httpStr;
}

public static String doPostByJsons(String apiUrl, List list,String token) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    String httpStr = null;
    HttpPost httpPost = new HttpPost(apiUrl);

// List lists = new LinkedList<>();
// BasicNameValuePair param1 = new BasicNameValuePair(“username”, “acrel”);
// BasicNameValuePair param2 = new BasicNameValuePair(“password”, “acrel001”);
// BasicNameValuePair param3 = new BasicNameValuePair(“userid”, “47”);
// list.add(param1);
// list.add(param2);
// list.add(param3);

    try {
        UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
        httpPost.setEntity(entityParam);
        httpPost.setEntity(entityParam);
        // 浏览器表示
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
        // 传输的类型
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        httpPost.addHeader("mark", "relay");
        //httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
        if (token!=null&&token!=""){
            httpPost.addHeader("token", token);
        }

        // 执行请求
        response = httpClient.execute(httpPost);
        // 获得响应的实体对象
        HttpEntity entity = response.getEntity();
        // 使用Apache提供的工具类进行转换成字符串
        httpStr = EntityUtils.toString(entity, "UTF-8");


    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (response != null) {
            try {
                EntityUtils.consume(response.getEntity());
                response.close();
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return httpStr;
}

public static String doPostByJsonss(String apiUrl, List list,String token) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    CloseableHttpResponse response = null;
    String httpStr = null;
    HttpPost httpPost = new HttpPost(apiUrl);

// List lists = new LinkedList<>();
// BasicNameValuePair param1 = new BasicNameValuePair(“username”, “acrel”);
// BasicNameValuePair param2 = new BasicNameValuePair(“password”, “acrel001”);
// BasicNameValuePair param3 = new BasicNameValuePair(“userid”, “47”);
// list.add(param1);
// list.add(param2);
// list.add(param3);

    try {
        UrlEncodedFormEntity entityParam = new UrlEncodedFormEntity(list, "UTF-8");
        httpPost.setEntity(entityParam);
        httpPost.setEntity(entityParam);
        // 浏览器表示
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
        // 传输的类型
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        httpPost.addHeader("mark", "relay");
        //httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.6)");
        if (token!=null&&token!=""){
            httpPost.addHeader("token", token);
        }

        // 执行请求
        response = httpClient.execute(httpPost);
        // 获得响应的实体对象
        HttpEntity entity = response.getEntity();
        // 使用Apache提供的工具类进行转换成字符串
        httpStr = EntityUtils.toString(entity, "UTF-8");


    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (response != null) {
            try {
                EntityUtils.consume(response.getEntity());
                response.close();
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return httpStr;
}

// public static void close(){
// try {
// response.close();
// httpclient.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值