web项目 调用外部接口 例子

这是一个Java程序,演示了如何使用Apache HttpClient库发送POST请求到外部HTTP接口。程序创建了一个HttpPost对象,设置了请求参数,并将JSON数据作为请求体发送。响应数据被读取并打印出来。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
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.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

public class dtservice {

	public static void main(String[] args) throws Exception  {
		String requestUrl = "http://******/**/service/um/third/***.do";
		HttpPost http = new HttpPost(requestUrl);
		JSONObject caInfoJson = new JSONObject();
		
				try {
			BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
			CloseableHttpClient httpclient = HttpClients.custom()
					.setDefaultCredentialsProvider(credsProvider)
					.build();
			;
			
			caInfoJson.put("OLD_ZSWYBS", "*****"); //接口参数
			
			NameValuePair pair1 = new BasicNameValuePair("jsonStr",
					caInfoJson.toString());
			List
    
    
     
      list = new ArrayList<>();
			list.add(pair1);
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(
					list, "utf-8");
			http.setEntity(entity);
			String reslutDeptUser = printRestData(httpclient, http);
			
			System.out.println(reslutDeptUser);//reslutDeptUser 返回结果
		} catch (Exception e) {
			
			e.printStackTrace();
		}
	}

	/**
	 * post请求。
	 * 
	 * @param httpclient
	 * @param http
	 * @return reslut 回文。
	 * @throws
	 * @throws Exception
	 */
	public static String printRestData(CloseableHttpClient httpclient,
			HttpPost http) throws Exception {
		CloseableHttpResponse response = null;
		
		InputStream in = null;
		InputStreamReader rd = null;
		BufferedReader br = null;
		String reslut = "";
		response = httpclient.execute(http);
		HttpEntity entity = response.getEntity();
		in = entity.getContent();
		rd = new InputStreamReader(in, "UTF-8");
		br = new BufferedReader(rd);
		String line = br.readLine();
		if (line != null) {
			while (true) {
				reslut = reslut.concat(line);
				line = br.readLine();
				if (line == null) {
					break;
				}
			}
		}
		// reslut = unicodeToUtf8(reslut);
		if (entity != null) {
			System.out.println("Response content length: "
					+ entity.getContentLength());
		}
		http.abort();

		try {
			if (response != null) {
				response.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (in != null) {
				in.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (rd != null) {
				rd.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			if (br != null) {
				br.close();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			httpclient.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return reslut;
	}
}
    
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值