Java 调用医保接口 实例代码
时间: 2023-10-09 12:09:53 浏览: 226
以下是 Java 调用医保接口的示例代码:
```java
import java.net.*;
import java.io.*;
public class MedicalInsuranceAPI {
public static void main(String[] args) {
String apiUrl = "http://api.yibaifan.com/medical_insurance";
String requestData = "{'name': '张三', 'id_card': '1234567890', 'medical_card': '0987654321'}";
try {
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/json");
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestData.getBytes());
outputStream.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String line;
StringBuffer response = new StringBuffer();
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
System.out.println(response.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
```
其中,`apiUrl` 是医保接口的地址,`requestData` 是请求数据,需要按照接口文档中的要求进行格式化。然后根据接口要求,设置请求方法为 POST,设置请求头的 Content-Type 为 application/json,然后将请求数据写入请求体中。发送请求后,读取响应数据并输出。
需要注意的是,该示例代码仅供参考,具体的接口调用方式需要根据实际情况进行调整。同时,医保接口的调用需要遵守相关法律法规和政策规定。
阅读全文