问题
we have a simple REST request that I can use manually on my Mac with RESTClient (from wiztools). The url is http://ws-argos.clsamerica.com/argosDws/services/DixService?getXml and the body is below:
<soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:typ="http://service.dataxmldistribution.argos.cls.fr/types">
<soap:Body>
<typ:xmlRequest>
<typ:username>******</typ:username>
<typ:password>******</typ:password>
<typ:platformId>62518,62688,62520,62602,62608</typ:platformId>
<typ:nbDaysFromNow>10</typ:nbDaysFromNow>
</typ:xmlRequest>
</soap:Body>
</soap:Envelope>
This returns a neat bit of XML with plenty of real data. So I know the data are there to be grabbed. However, I want to automate this on a nightly script in Linux, and am trying to use CURL for this using the two scripts below:
curl -H "content-type: application/soap+xml" \
-H "SOAPAction:" \
-d@soap.xml \
-X POST http://ws-argos.clsamerica.com/argosDws/services/DixService?getXml \
> output.xml
which calls soap.xml with:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:typ="http://service.dataxmldistribution.argos.cls.fr/types">
<soap:Body>
<typ:xmlRequest>
<typ:username>******</typ:username>
<typ:password>******</typ:password>
<typ:platformId>62518,62688,62520,62602,62608</typ:platformId>
<typ:nbDaysFromNow>10</typ:nbDaysFromNow>
</typ:xmlRequest>
</soap:Body>
The output is:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope">
<soap:Body><soap:Fault>
<soap:Code><soap:Value>soap:Sender</soap:Value></soap:Code>
<soap:Reason><soap:Text xml:lang="en">Error reading XMLStreamReader.</soap:Text>
</soap:Reason>
</soap:Fault></soap:Body></soap:Envelope>
Does anyone know how to resolve this, or a better way to automate it? I'm new to CURL, but could create something in Java if anyone suggests that as possibility (with examples ;-))
回答1:
I haven't used Curl but since the author of the question would be happy with Java code, see below. I've followed this example: How to Build a SOAP Client on Google App Engine
Here's a script you can use to generate code for the Argos Web Service:
wsdluri='https://ws-argos.cls.fr/argosDws/services/DixService?wsdl'
gensrcdir='./src'
targetpackage='com.mypackage'
genoutdir='./war/WEB-INF/classes'
wsimport -extension -d "$genoutdir" -s "$gensrcdir" -p $targetpackage -keep "$wsdluri"
Here's a simple test for the generated code:
@Test
public void test_parse() throws IOException, DixException_Exception {
try {
DixServicePortType argos = new DixService().getDixServicePort();
CsvRequestType params = new CsvRequestType();
params.setShowHeader(true);
params.setUsername("your_user_name");
params.setPassword("your_password");
params.setDisplaySensor(true);
// params.setProgramNumber("your_program_number");
params.setPlatformId("your_device_id");
params.setNbDaysFromNow(2);
StringResponseType csvres = argos.getCsv(params);
assertNotNull(csvres);
System.out.println(csvres.getReturn());
StringResponseType xmlres = argos.getXml(params);
assertNotNull(xmlres);
System.out.println(xmlres.getReturn());
} catch (SOAPFaultException e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Note, although I've pointed the web service to an https address, it seems like the generated code still connects via http. I'd like to try https but I'll have to change it manually.
Now I'd like to use Jaxb to parse the XML response. I can deal with the CSV output, it's simple enough but since it's soap, we may as well do everything the same way. Any suggestion is welcome.
[Edit] I've succeeded getting the schema for XML data and generating classes with the "xjc" compiler for unmarshalling via jaxb. Getting the schema is as simple as:
StringResponseType xmlSchema = argos.getXsd(new XsdRequestType());
assertNotNull(xmlSchema);
System.out.println(xmlSchema.getReturn());
// save schema to argos.xsd file
Then you can invoke "xjc" to generate classes:
xjc -p com.mypackage argos.xsd
Unfortunately there's a problem I've had before, the case of missing "XmlRootElement" annotations. Lots of SO questions to that regard. As far as I understand, it's a matter of forcing "jxc" to treat certain names as unique (see also this article). Because I'd rather not change the schema, I've tried setting up "jxc" via an external bindings configuration file. That didn't work but it has worked for other people. So at the end, I made some small manual changes to the schema that have added XmlRootElement annotations to the generated classes:
<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings>
<xjc:simple />
</jaxb:globalBindings>
</xs:appinfo>
</xs:annotation>
<xs:element name="data" type="data"/>
Now I can parse the XML data using Jaxb. Here's an example with Restlet's jaxb connector:
ConverterHelper decoder = new JaxbConverter();
Data data = decoder.toObject(new StringRepresentation( xmlres.getReturn()), Data.class, null);
Hope that helps, if you have problems with this solution, please comment.
来源:https://stackoverflow.com/questions/9003761/simple-curl-failing-from-linux-command-line