java调用第三方接口post请求,josn返回与解析

1.拿到接口文档

首先我们会得到第三方公司的接口文档,我们会依据其中的规范去做请求的参数或者获取加密手段

2.代码实践

需求
请求体为json格式

package com.seeyon.apps.hd.controller;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import net.sf.json.JSONObject;

public class JSONJavaHttpDemo {
	//传递url
	public static final String ADD_URL = " 这里填写接口文档访问的url";

	public static void appadd() {

		try {
			// 创建连接
			URL url = new URL(ADD_URL);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.setRequestMethod("POST");
			connection.setUseCaches(false);
			connection.setInstanceFollowRedirects(true);
			//设置请求头
			connection.setRequestProperty("Content-Type", "application/json");

			connection.connect();

			// POST请求,包装成json数据
			DataOutputStream out = new DataOutputStream(connection.getOutputStream());
			JSONObject obj = new JSONObject();
			obj.element("app_name", "asdf");
			obj.element("app_ip", "10.192.203.46");
			obj.element("app_port", 8080);
			obj.element("app_type", "001");
			obj.element("app_area", "asd");

			out.writeBytes(obj.toString());
			out.flush();
			out.close();

			// 读取响应
			BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
			String lines;
			StringBuffer sb = new StringBuffer("");
			while ((lines = reader.readLine()) != null) {
				lines = new String(lines.getBytes(), "utf-8");
				sb.append(lines);
			}
			System.out.println(sb);
			reader.close();
			// 断开连接
			connection.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void main(String[] args) {
		appadd();
	}

}

  • 3
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTTP POST请求可以用来向服务器提交各种类型的数据,包括JSON格式数据。在Java中,我们可以使用HttpClient库中的HttpPost类和HttpEntity类来发送JSON格式的HTTP POST请求。以下是一个简单的示例: ```java HttpPost httpPost = new HttpPost("http://www.example.com/submit"); StringEntity entity = new StringEntity("{\"name\":\"John\",\"age\":30,\"city\":\"New York\"}", ContentType.APPLICATION_JSON); httpPost.setEntity(entity); CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = httpClient.execute(httpPost); ``` 在这个示例中,我们创建了一个HttpPost对象,设置了请求URL和请求体。请求体使用了StringEntity类来封装JSON格式的数据,并设置Content-Type为application/json。然后使用HttpClient库发送HTTP POST请求,并获取响应结果。 另外,我们也可以使用第三方库如Google的GSON库来将Java对象转换为JSON格式的字符串,然后发送HTTP POST请求。例如: ```java HttpPost httpPost = new HttpPost("http://www.example.com/submit"); User user = new User("John", 30, "New York"); String json = new Gson().toJson(user); StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); httpPost.setEntity(entity); CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse response = httpClient.execute(httpPost); ``` 在这个示例中,我们创建了一个User对象,并使用GSON库将其转换为JSON格式的字符串。然后将该字符串设置为请求体,并发送HTTP POST请求
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值