package chapter_14_solution;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
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.HttpClientBuilder;
import org.apache.http.util.EntityUtils;

public class HttpDemoApache {

    public static void main(String[] args) throws Exception {
        // 创建HttpClient对象
        HttpClient httpClient = HttpClientBuilder.create().build();
        // 创建一个HttpPost对象，指定请求的URL
        // http://123.56.60.184:8888/examine
        // http://123.56.60.184:8888/examRptPattern
        HttpPost httpPost = new HttpPost("http://123.56.60.184:8888/examRptPattern");

        // 设置请求头
        String jsonString = "{\"ExamClass\":\"彩超\", \"ExamSubClass\":\"血管彩超\"}";
        // 创建一个StringEntity对象，并设置请求体
        StringEntity stringEntity = new StringEntity(jsonString, ContentType.create("application/json", "UTF-8"));
        // 设置请求体
        httpPost.setEntity(stringEntity);

        // 发送POST请求并获取响应
        HttpResponse response = httpClient.execute(httpPost);
        // 打印响应状态码和响应体
        String responseBody = EntityUtils.toString(response.getEntity());
        // 打印响应体
        System.out.println(responseBody);
    }
}
