succtrue Header ActionhttpwwwdongguanbankcnG012000602 Address0332 EsbHeader VrsNo100 ScnNo10 SrcDate20190903 SrcTime165025 SrcSysId0332 SrcCalCodUM
可以使用Java中的HttpClient库来发送HTTP请求,具体实现如下:
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
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.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
public class HttpClientExample {
public static void main(String[] args) throws IOException {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("http://www.dongguanbank.cn/G0120006/02");
String json = "{\"succ\":true,\"Header\":{\"Action\":\"http://www.dongguanbank.cn/G0120006/02\",\"Address\":\"0332\"},\"EsbHeader\":{\"VrsNo\":\"100\",\"ScnNo\":\"10\",\"SrcDate\":\"20190903\",\"SrcTime\":\"165025\",\"SrcSysId\":\"0332\",\"SrcCalCod\":\"UMP\",\"GloSeqNo\":\"G033220190903000000000004\",\"ReqNo\":\"G033220190903000000000004\"},\"APPHeader\":null,\"APPBody\":{\"code\":0,\"msg\":\"成功\",\"uuid\":\"8b3ff1a6-fb15-4893-8064-bac4118fc1b2\",\"data\":null}}";
httpPost.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
CloseableHttpResponse httpResponse = httpClient.execute(httpPost);
try {
HttpEntity entity = httpResponse.getEntity();
String response = EntityUtils.toString(entity);
EntityUtils.consume(entity);
System.out.println(response);
} finally {
httpResponse.close();
}
}
}
其中,首先创建一个CloseableHttpClient对象,然后创建HttpPost对象,指定请求的URL地址,并将JSON字符串作为请求的body内容。最后,发送HTTP请求并获取响应内容。注意,需要在finally块中关闭响应。
原文地址: https://www.cveoy.top/t/topic/8an 著作权归作者所有。请勿转载和采集!