Java 使用ApacheCamel对USPSAPI进行地址验证的HTTP查询

Java 使用ApacheCamel对USPSAPI进行地址验证的HTTP查询,java,xmlhttprequest,apache-camel,usps,Java,Xmlhttprequest,Apache Camel,Usps,我已经编写了使用camel调用USPS地址验证api的代码。 我尝试了所有可能的方法来发送请求,但是它被编码了,请求失败了。当USPS API的端点需要xml字符串时,由于对请求进行编码,xml端点无法解析该请求并出现解析错误 <?xml version="1.0" encoding="UTF-8"?> <Error> <Number>80040B19</Number> <Description>XML Syntax Error

我已经编写了使用camel调用USPS地址验证api的代码。 我尝试了所有可能的方法来发送请求,但是它被编码了,请求失败了。当USPS API的端点需要xml字符串时,由于对请求进行编码,xml端点无法解析该请求并出现解析错误

<?xml version="1.0" encoding="UTF-8"?>
<Error>
  <Number>80040B19</Number>
  <Description>XML Syntax Error: Please check the XML request to see if it can be parsed.</Description>
  <Source>USPSCOM::DoAuth</Source>
</Error>

80040B19
XML语法错误:请检查XML请求以查看是否可以对其进行解析。
USPSCOM::DoAuth
请求的编码方式与签入wireshark时类似:

/ShippingApiest.dll?API=Verify&XML=%3CAddressValidateRequest%20USERID=%5C%22705LBEYO2631%5C%22%3E%3E%3E%3C%3C Address1%3E%3C/Address1%3E%3CAddress2%3E6406%20Ivy%20Lane%3C/Address2%3E%3CCity%3E%3CState%3EMD%3C%3C/State%3E%3CZip5%3E?%3E%3CZip5%3E%3CZip5%3E%3C%3CZip5%3E%3c3c3e%3c3c/3C%3CZip5%3C%3E%3C%3C%3C%3E%3CZip5%3E%3C%3E%3C/3C%

其预期格式为:

API=Verify&XML=<AddressValidateRequest%20USERID=\"705LBEYO2631\"><Address><Address1></Address1><Address2>6406 Ivy Lane</Address2><City>Greenbelt</City><State>MD</State><Zip5></Zip5><Zip4></Zip4></Address></AddressValidateRequest>
API=Verify&XML=
ServiceUspsAddressValidationProcessor-->ConstructAddressValidationRequest()

方法包含以下代码片段:

exchange.setProperty(Exchange.HTTP_METHOD, "GET");
//exchange.setProperty(Exchange.CONTENT_TYPE, "text/xml");
exchange.getIn().setHeader(Exchange.HTTP_QUERY,  getHttpQuery(serviceUspsAddressValidationRequest.getUspsAddress()));
exchange.getIn().setHeader(Exchange.HTTP_QUERY, getHttpQuery(serviceUspsAddressValidationRequest.getUspsAddress()));
exchange.getIn().setBody("");

...

public String getHttpQuery(UspsAddress uspsAddress){
  String validateUrl = "API=Verify&XML=<AddressValidateRequest USERID=\\\"%s\\\"><Address><Address1>%s</Address1><Address2>%s</Address2><City>%s</City><State>%s</State><Zip5>%s</Zip5><Zip4>%s</Zip4></Address></AddressValidateRequest>";
  String userid = "XXXXXXXXXXXX" ;  
  String httpQuery = String.format(validateUrl, userid,
  uspsAddress.getAddress1(),
  uspsAddress.get_Address2(),
  uspsAddress.getCity(),
  uspsAddress.getState(),
  uspsAddress.getZip(),
  uspsAddress.getZipPlus4());
  //httpQuery = StringEscapeUtils.escapeJava(httpQuery);
  System.out.println("\n****************httpQuery ****************\n" + httpQuery);
  return httpQuery;
}
exchange.setProperty(exchange.HTTP_方法,“GET”);
//setProperty(exchange.CONTENT_类型,“text/xml”);
exchange.getIn().setHeader(exchange.HTTP_查询,getHttpQuery(serviceUspsAddressValidationRequest.getUspsAddress());
exchange.getIn().setHeader(exchange.HTTP_查询,getHttpQuery(serviceUspsAddressValidationRequest.getUspsAddress());
exchange.getIn().setBody(“”);
...
公共字符串getHttpQuery(UspsAddress UspsAddress){
String validateUrl=“API=Verify&XML=%s%s%s%s%s”;
字符串userid=“xxxxxxxxxxx”;
String httpQuery=String.format(validateUrl、userid、,
uspsAddress.getAddress1(),
uspsAddress.get_Address2(),
uspsAddress.getCity(),
uspsAddress.getState(),
uspsAddress.getZip(),
uspsAddress.getZipPlus4();
//httpQuery=StringEscapeUtils.escapeJava(httpQuery);
System.out.println(“\n***************************************************\n”+httpQuery);
返回httpQuery;
}
exchange.setProperty(Exchange.HTTP_METHOD, "GET");
//exchange.setProperty(Exchange.CONTENT_TYPE, "text/xml");
exchange.getIn().setHeader(Exchange.HTTP_QUERY,  getHttpQuery(serviceUspsAddressValidationRequest.getUspsAddress()));
exchange.getIn().setHeader(Exchange.HTTP_QUERY, getHttpQuery(serviceUspsAddressValidationRequest.getUspsAddress()));
exchange.getIn().setBody("");

...

public String getHttpQuery(UspsAddress uspsAddress){
  String validateUrl = "API=Verify&XML=<AddressValidateRequest USERID=\\\"%s\\\"><Address><Address1>%s</Address1><Address2>%s</Address2><City>%s</City><State>%s</State><Zip5>%s</Zip5><Zip4>%s</Zip4></Address></AddressValidateRequest>";
  String userid = "XXXXXXXXXXXX" ;  
  String httpQuery = String.format(validateUrl, userid,
  uspsAddress.getAddress1(),
  uspsAddress.get_Address2(),
  uspsAddress.getCity(),
  uspsAddress.getState(),
  uspsAddress.getZip(),
  uspsAddress.getZipPlus4());
  //httpQuery = StringEscapeUtils.escapeJava(httpQuery);
  System.out.println("\n****************httpQuery ****************\n" + httpQuery);
  return httpQuery;
}