使用apache camel

时间:2015-06-11 11:09:16

标签: java xmlhttprequest apache-camel usps

我使用camel编写代码来调用USPS地址validaton 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>

请求被编码,就像在wireshark中检查一样:

  

/ShippingAPITest.dll?API=Verify&XML=%3CAddressValidateRequest%20USERID=%5C%22705LBEYO2631%5C%22%3E%3CAddress%3E%3CAddress1%3E%3C/Address1%3E%3CAddress2%3E6406%20Ivy% 20Lane%3C /地址2%3E%3CCity%3EGreenbelt%3C /市%3E%3CState%3EMD%3C /州%3E%3CZip5%3E?%3C / Zip5%3E%3CZip4%3E?%3C / ZIP4%3E% 3C /地址%3E%3C / AddressValidateRequest%3E HTTP / 1.1

虽然其预期格式为:

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>

路由文件:route.xml

<route id="UspsAddressValidationRoute" >
  <from id="ProcessAddressValidationRequest" uri="direct:processUspsAddressValidation" />
  <to id="PrepareAddressValidationRequest" uri="bean:serviceUspsAddressValidationProcessor?method=constructAddressValidationRequest" />
  <setHeader headerName="CamelHttpMethod">
    <constant>GET</constant>
  </setHeader>
  <to id="SendAddressValidationRequestToUsps" uri="{{usps.soapEndpoint}}"/>  
  <to id="ProcessAddressValidationUspsResponse" uri="bean:serviceUspsAddressValidationProcessor?method=processUspsAddressValidationResponse" />
</route>

usps.soapEndpoint = http://testing.shippingapis.com/ShippingAPITest.dll
ServiceUspsAddressValidationProcessor - &gt; 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;
}

0 个答案:

没有答案