import org.apache.http.*;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
public class xiehuiTest {
@Test
public void testPost() throws Exception{
String urlStr = "http://";
CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
//构造httpGet对象
HttpPost httpPost = new HttpPost(urlStr);
//设置参数
List<NameValuePair> list = new ArrayList<>();
list.add(new BasicNameValuePair("xml",getXmlInfo()));
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(list, StandardCharsets.UTF_8);
httpPost.setEntity(formEntity);
//构造响应对象
CloseableHttpResponse response = null;
try{
response = closeableHttpClient.execute(httpPost);
//获取响应头
Header[] allHeaders = response.getAllHeaders();
for(Header head: allHeaders){
System.out.println("响应头名称: " + head.getName() + ", 值为: " + head.getValue());
}
//代表本地请求的成功,失败的状态
StatusLine statusLine = response.getStatusLine();
if(HttpStatus.SC_OK == statusLine.getStatusCode()){
//200表示成功
System.out.println("响应成功>>>>>>>>>>>>>>>>>");
//获取响应结果
HttpEntity entity = response.getEntity();
//获取内容类型
System.out.println("内容类型: " + entity.getContentType());
//对HttpEntity操作的工具类
String toStringResult = EntityUtils.toString(entity, StandardCharsets.UTF_8);
System.out.println("toStringResult: " + toStringResult);
//确保流关闭
EntityUtils.consume(entity);
}else{
System.out.println("响应失败,响应码:>>>>>> " +statusLine.getStatusCode());
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(closeableHttpClient != null){
try{
closeableHttpClient.close();
}catch (Exception e){
e.printStackTrace();
}
}
if( response != null){
try{
response.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
private String getXmlInfo() {
StringBuilder sb = new StringBuilder();
sb.append("<?xml version='1.0' encoding='UTF-8'?>");
sb.append("<Document>");
sb.append("<Request>");
sb.append(" <Head>");
sb.append(" <Version>V1.3.0</Version>");
sb.append(" <Identification>202207120000671231</Identification>");
sb.append(" <OrigSender></OrigSender>");
sb.append(" <OrigSenderSID></OrigSenderSID>");
sb.append(" <RecSystemId>R0001</RecSystemId>");
sb.append(" <TrnxCode>TS0001</TrnxCode>");
sb.append(" <TrnxTime>20220712121436</TrnxTime>");
sb.append(" <SecretKey>eKjF8eUrkzyWhAyr+RhTQpZZ7B7YhT289vLLSvEGEUR4gR5qOwQv16ituKMUOc5jZzBuUjNUD7AEubJZTkIr8MTk8JoiZ+iRmPMusi89yM9jnsrvlI3K0Bfi1aGkAHlJgKLZxqcVmVwElglg4CkXKJc8NEubjLXFTbaVfecla5rTyfbWfmuXOm71Gdcc3Y6u+bvqvFL67JKSmzqgn8GrsxS4AMO74h7woW4ABnBLHyhV8djuD4qLZblKh9ZhvE8lxkFoyg7fprg7/cJn8LO9uzcVVA7CFXvu96i6ZA9h+DCmTkzkwZYWuVm1E6xcS+fYiQVhKJIQE4/Ci+qnXiFEgw==</SecretKey>");
sb.append(" </Head>");
sb.append(" <Body>");
sb.append(" </Body>");
sb.append("</Request>");
sb.append("<Signature>");
sb.append("M0EcMjMMAMddhQdsJ+ETFMHa1CG8hYonVtJHdoYCMRGOsSqvpjX8ysqn7SQ6LfsIIRAsQ39PWjKxvXDH3LlZUeU1BAz/3T/IN4yIEqXURGeC66WXMMel3ZlZQDHrcm0tHcr8x2L99anA/DJThDceiKdfWZzDNW700su7JmBu5aEVGEwOEgXAKyhH7vhK1VqfinK2M5RAXdkTppQ6kNZ2G3zI11+JaH4fYhyskqROQJXGfLFSTXbhdv+5ZlsMJ1RmScYQRnyTNzIQ095LPJmf3cYfLDn2ifzFjzQqdl9lhwfqcDezA8IkG/TRE7tNbUkHwjAuEUXaLpEj4VOy5hooUA==");
sb.append("</Signature>");
sb.append("</Document>");
return sb.toString();
}
}
45 协会test
于 2022-07-12 14:41:55 首次发布