httpclient 聚合


依赖

<dependency>
     <groupId>org.apache.httpcomponents</groupId>
     <artifactId>httpclient</artifactId>
     <version>4.5.3</version>
 </dependency>

 <dependency>
     <groupId>org.apache.httpcomponents</groupId>
     <artifactId>httpmime</artifactId>
     <version>4.5.3</version>
 </dependency>

DefaultHttpClient 废弃

  • DefaultHttpClient httpclient = new DefaultHttpClient ();
  • 使用 CloseableHttpClient client = HttpClients.createDefault(); 替换

设置代理

HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97 Safari/537.36");
        HttpHost proxy = new HttpHost("127.0.0.1",8008);
        RequestConfig requestConfig = RequestConfig.custom()
                .setProxy(proxy)
                .setConnectTimeout(10000)
                .setSocketTimeout(10000)
                .setConnectionRequestTimeout(3000)
                .build();
        httpPost.setConfig(requestConfig);

传文件

  • 转载于:http://hc.apache.org/httpcomponents-client-4.5.x/httpmime/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java
import java.io.File;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * Example how to use multipart/form encoded POST request.
 */
public class ClientMultipartFormPost {

    public static void main(String[] args) throws Exception {
        if (args.length != 1)  {
            System.out.println("File path not given");
            System.exit(1);
        }
        CloseableHttpClient httpclient = HttpClients.createDefault();
        try {
            HttpPost httppost = new HttpPost("http://localhost:8080" +
                    "/servlets-examples/servlet/RequestInfoExample");

            FileBody bin = new FileBody(new File(args[0]));
            StringBody comment = new StringBody("A binary file of some kind", ContentType.TEXT_PLAIN);

            HttpEntity reqEntity = MultipartEntityBuilder.create()
                    .addPart("bin", bin)
                    .addPart("comment", comment)
                    .build();


            httppost.setEntity(reqEntity);

            System.out.println("executing request " + httppost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                System.out.println("----------------------------------------");
                System.out.println(response.getStatusLine());
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    System.out.println("Response content length: " + resEntity.getContentLength());
                }
                EntityUtils.consume(resEntity);
            } finally {
                response.close();
            }
        } finally {
            httpclient.close();
        }
    }

}

HttpClientUtil (version=4.3.6 )

<dependency>
	<groupId>org.apache.httpcomponents</groupId>
	<artifactId>httpclient</artifactId>
	<version>4.3.6</version>
</dependency>
import org.apache.http.HttpEntity;
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.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringWriter;

public class HttpClientUtil {


    public static String doPost(String url, String jsonStr){
        StringEntity stringEntity = new StringEntity(jsonStr, ContentType.APPLICATION_JSON);
        return doPost(url, stringEntity);
    }

    public static String doPost(String url, HttpEntity entity){
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json; charset=UTF-8");
        //httpPost.addHeader("Accept", " application/json");
//        List<NameValuePair> list = new ArrayList<NameValuePair>();
//        list.add(new BasicNameValuePair("accountSecret",accountSecret));
//        UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(list, "UTF-8");
        httpPost.setEntity(entity);
        // 发起请求
        CloseableHttpResponse response = null;
        String result = null;
        try {
            response = client.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();

            result = EntityUtils.toString(response.getEntity());
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(response != null){
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    public static String doGet(String url){
        CloseableHttpClient client = HttpClients.createDefault();
       // HttpClient httpClient = new HttpClient();
        if(url ==null){
        	return null;
        }
        HttpGet getMethod = new HttpGet(url);
        // 发起请求
        CloseableHttpResponse response = null;
        String result = null;
        try {
            response = client.execute(getMethod);
            int statusCode = response.getStatusLine().getStatusCode();

            HttpEntity entity = response.getEntity();
            if(entity !=null){
            	InputStream in = entity.getContent();
            	StringWriter sw = new StringWriter();
                SAXReader xmlReader = new SAXReader();
                Document doc= xmlReader.read(new InputStreamReader(in,"UTF-8"));
                XMLWriter writer_sw = new XMLWriter(sw);
                writer_sw.write(doc);
                result = sw.toString();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            try {
                if(response != null){
                    response.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现建设银行聚合支付生成订单接口,可以按照以下步骤进行: 1. 导入建设银行支付接口的开发。在Java项目中,可以通过使用Maven或Gradle等构建工具,将建设银行支付接口的依赖添加到项目中。 2. 创建一个Java类来处理生成订单的逻辑。可以命名为"CcbPaymentService"或类似的名称。在该类中,可以定义一个方法名为"generateOrder"用于生成订单。 3. 在"generateOrder"方法中,首先要通过建设银行支付接口提供的API,构建生成订单的请求参数。这些参数通常括商户号、支付金额、订单号、交易描述等。 4. 调用建设银行支付接口的生成订单方法,将构建好的请求参数传递给该方法。可以通过使用Java的网络请求库,如HttpClient或OkHttp,向建设银行支付接口发送POST请求。 5. 接收建设银行支付接口返回的结果,并对结果进行处理。可以通过解析返回的JSON数据,获取生成的订单号或其他相关信息。 6. 返回生成的订单号或其他相关信息给调用方。可以将该信息封装成一个Java对象,方便后续的使用和传递。 7. 在调用方的其他逻辑中,可以使用这个生成的订单号来进行后续的支付流程。 以上是实现建设银行聚合支付生成订单接口的大致步骤。具体的实现细节会根据建设银行支付接口的具体要求而有所不同。在实际开发中,还需要进行异常处理、参数校验、日志记录等工作,以确保代码的健壮性和稳定性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值