场景
在SSM项目中的后台Controller中需要想asp的接口发送xml格式的请求参数。
依据接口文档示例代码。
入参:
<Request>
<ServiceCode>get_fight</ServiceCode>
<Pid>111111</Pid>
<UserName>badao</UserName>
<Sign>222222</Sign>
<Params>
<DepartCity>SHA</DepartCity>
<ArriveCity>SIN</ArriveCity>
<StartDate>2019-07-23</StartDate>
</Params>
</Request>
实现
项目中引入pom依赖
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.6.0</version>
</dependency>
实现代码
public static String GJticketBookJsonStr(GJBookParam bookParam) throws ParseException, IOException {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date now = new Date();
String strNow = sdf.format(now);
//使用StringBufer来拼接xml格式参数
StringBuffer sbParams = new StringBuffer();
//使用append方法来追加公共参数
sbParams.append("<Flight>");
sbParams.append("<DepartCity>").append(bookParam.getFlightInfo().getDepartCity()).append("</DepartCity>");
sbParams.append("<ArriveCity>").append(bookParam.getFlightInfo().getArriveCity()).append("</ArriveCity>");
sbParams.append("<StartDate>").append(bookParam.getFlightInfo().getStartDate()).append("</StartDate>");
sbParams.append("<SeqNo>").append(bookParam.getFlightInfo().getSeqNo()).append("</SeqNo>");
sbParams.append("</Flight>");
sbParams.append("<PassengerInfo>");
//通过循环拼接多个参数节点
for(ParamGJPassenger passenger : bookParam.getPassengerInfo()) {
sbParams.append("<Passenger>");
sbParams.append("<SurName>").append(passenger.getSurName()).append("</SurName>");
sbParams.append("<Name>").append(passenger.getName()).append("</Name>");
sbParams.append("<PassType>").append(passenger.getPassType()).append("</PassType>");
sbParams.append("<Sex>").append(passenger.getSex()).append("</Sex>");
sbParams.append("<IdType>").append(passenger.getIdType()).append("</IdType>");
sbParams.append("<IdDate>").append(passenger.getIdDate()).append("</IdDate>");
sbParams.append("<Id>").append(passenger.getId()).append("</Id>");
sbParams.append("<Country>").append(passenger.getCountry()).append("</Country>");
sbParams.append("<CountryCode>").append(passenger.getCountryCode()).append("</CountryCode>");
sbParams.append("<Birthdays>").append(passenger.getBirthdays()).append("</Birthdays>");
sbParams.append("</Passenger>");
}
sbParams.append("</PassengerInfo>");
//将多个参数节点格式化进请求参数中
String sign = "%s%s%s%s%s";
sign = String.format(sign, sbParams.toString(), "1111", ShiroHelper.md5Encrypt("111111", null, null, 1).toUpperCase(), strNow, "11111111");
sign = ShiroHelper.md5Encrypt(sign, null, null, 1).toUpperCase();
String request = "<Request><ServiceCode>%s</ServiceCode><Pid>%s</Pid><UserName>%s</UserName><Sign>%s</Sign><Params>%s</Params></Request>";
request = String.format(request, "jiekouName", "111111", "1111111", sign, sbParams.toString());
System.out.println(request);
//客户端对象
OkHttpClient client = new OkHttpClient();
//请求体
RequestBody body = RequestBody.create(MediaType.parse("application/xml;charset=utf-8"), request);
//请求对象 url 请求头 post方式
Request req = new Request.Builder().url("http://127.0.0.1:8888/test.aspx").header("Content-Type", "application/xml").post(body).build();
//发送请求并接受收响应
Response response = client.newCall(req).execute();
if (!response.isSuccessful())
{
throw new IOException("Unexpected code " + response);
}
String jsonResult = response.body().string();
return jsonResult;
}
断点效果