简单的curl从linux命令行失败 [英] Simple curl failing from linux command line

查看:1640
本文介绍了简单的curl从linux命令行失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个简单的REST请求,我可以使用手动在我的Mac上使用RESTClient(从wiztools)。网址为 http://ws-argos.clsamerica.com/argosDws/services/ DixService?getXml ,正文如下:

 &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>

这会返回一个整齐的XML和大量真实数据。所以我知道数据有被抓住。但是,我想在Linux中的每天的脚本自动化这个,我试图使用CURL这两个脚本如下:

  curl -Hcontent-type:application / soap + xml\ 
-HSOAPAction:\
-d@soap.xml \
-X POST http ://ws-argos.clsamerica.com/argosDws/services/DixService?getXml \
> output.xml

调用soap.xml时使用:

 <?xml version =1.0encoding =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>



输出为: p>

 &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>读取XMLStreamReader时出错。< / soap:Text>
< / soap:Reason>
< / soap:Fault>< / soap:Body>< / soap:Envelope>

有人知道如何解决这个问题,还是更好的自动化方法?我是CURL的新手,但是如果任何人认为是可能的话,可以在Java中创建一些东西(

解决方案

我没有使用Curl,但因为问题的作者会对Java代码感到满意,见下文。我遵循了以下示例:如何在Google App Engine上构建SOAP客户端

这里有一个脚本可用于为Argos Web服务生成代码:

  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

这是对生成的代码的简单测试:

  @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());
}
}

请注意,虽然我已将Web服务一个https地址,似乎生成的代码仍然通过http连接。我想尝试https,但我必须手动更改。



现在我想使用Jaxb来解析XML响应。我可以处理CSV输出,它很简单,但由于它是soap,我们也可以做同样的一切。
欢迎任何建议。




我已成功获得XML数据的模式并使用xjc编译器生成类通过jaxb解组。
获取模式的方法很简单:

  StringResponseType xmlSchema = argos.getXsd(new XsdRequestType()); 
assertNotNull(xmlSchema);
System.out.println(xmlSchema.getReturn());
//将模式保存到argos.xsd文件

然后你可以调用xjc生成类:

  xjc -p com.mypackage argos.xsd 
SO问题。
据我所知,这是一个强制jxc将某些名称视为唯一的名称(另见这篇文章)。因为我不想改变模式,我已经尝试通过外部绑定配置文件设置jxc。这没有工作,但它已经为其他人工作。
所以最后,我对模式进行了一些小的手动更改,为生成的类添加了XmlRootElement注释:

 < xs:annotation> 
< xs:appinfo>
< jaxb:globalBindings>
< xjc:simple />
< / xs:appinfo>
< / xs:annotation>

< xs:element name =datatype =data/>



现在我可以使用Jaxb解析XML数据。这里有一个Restlet的jaxb连接器的例子:

  ConverterHelper decoder = new JaxbConverter 
数据data = decoder.toObject(new StringRepresentation(xmlres.getReturn()),Data.class,null);

希望有帮助,如果您对此解决方案有问题,请予以注释。


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 ;-))

解决方案

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.

这篇关于简单的curl从linux命令行失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆