java实现post类型接口的调用

(一)相关jar包坐标

		<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.2</version>
		</dependency>

(二)编写代码

/**
 * json格式提交参数
 * @param uri 接口地址
 * @param params 
 */
	public static void doJsonPost(String uri, String params) {
		// 创建一个post请求
		HttpPost post = new HttpPost(uri);
		post.setHeader("X-Lemonban-Media-Type", "lemonban.v1");
		post.setHeader("Content-Type", "application/json");
		try {
			// 设置参数
			post.setEntity(new StringEntity(params, "utf-8"));
			// 创建客户端
			HttpClient httpClient = HttpClients.createDefault();
			// 发送请求
			HttpResponse response = httpClient.execute(post);
			getCodeAndResult(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 表单方式提交uri
	 * @param uri uri地址
	 * @param params  请求参数
	 */
	public static void doFormPost(String uri, String params) {
		// 创建一个post请求
		HttpPost post = new HttpPost(uri);
		post.setHeader("X-Lemonban-Media-Type", "lemonban.v1");
		post.setHeader("Content-Type", "application/x-www-form-urlencoded");
		try {
			// 设置参数
			post.setEntity(new StringEntity(params, "utf-8"));
			// 创建客户端
			HttpClient httpClient = HttpClients.createDefault();
			// 发送请求
			HttpResponse response = httpClient.execute(post);
			getCodeAndResult(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

		/**
	 * 根据响应结果获取状态码和body
	 * 
	 * @param response 响应结果
	 * @throws IOException
	 */
	private static void getCodeAndResult(HttpResponse response) throws IOException {
		// 获取状态码
		int code = response.getStatusLine().getStatusCode();
		System.out.println(code);
		// 获取body
		String result = EntityUtils.toString(response.getEntity());
		System.out.println(result);
	}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中,可以使用 HttpURLConnection 类来调用 POST 接口。以下是一个示例代码: ```java import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.nio.charset.StandardCharsets; public class HttpPostRequest { public static void main(String[] args) throws Exception { String url = "http://example.com/api/endpoint"; String requestBody = "{\"param1\":\"value1\",\"param2\":\"value2\"}"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // 设置请求方法为 POST con.setRequestMethod("POST"); // 设置请求头 con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); // 发送 POST 请求 con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.write(requestBody.getBytes(StandardCharsets.UTF_8)); wr.flush(); wr.close(); // 获取响应结果 BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuilder response = new StringBuilder(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); System.out.println(response.toString()); } } ``` 在上述代码中,我们首先定义了接口的 URL 和请求体。然后,我们创建了一个 HttpURLConnection 实例,并设置请求方法为 POST。接着,我们设置了请求头,并发送了请求体。最后,我们读取了响应结果并打印到控制台上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值