iOS中的NDFD SOAP请求

时间:2012-02-19 22:57:46

标签: cocoa-touch soap request ndfd

我正在尝试使用SOAP请求从国家数字预测数据库(NDFD)访问每日天气。 SOAP URL是

http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php

NDFDgenByDay()的肥皂操作是

http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#NDFDgenByDay. 

当我发送请求时,我收到的xml表示发生了错误,我认为它与我的HTTP标头有关。错误响应如下所示。

 <?xml version="1.0" encoding="ISO-8859-1"?><SOAP-ENV:Envelope SOAP-    ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body><SOAP-ENV:Fault><faultcode xsi:type="xsd:string">SERVER</faultcode>    <faultactor xsi:type="xsd:string"></faultactor><faultstring xsi:type="xsd:string">format needs to be either 24 hourly or 12 hourly</faultstring><detail xsi:type="xsd:string">input     format was &quot;&quot;</detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>

此错误反对“format”值,但如下所示,我的xml符合。

我在iOS / Obj-C中发送请求,如下所示:

    NSString *path = [[NSBundle mainBundle] pathForResource:@"soapRequest" ofType:@"xml"];
    NSString *soapContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];

    NSURL *url = [NSURL URLWithString:kWeatherSOAP_URL];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapContent length]];

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue:@"NDFDgenByDay" forHTTPHeaderField:@"Name"];
    [theRequest addValue:@"ndfdXMLBinding" forHTTPHeaderField:@"Binding"];
    [theRequest addValue:@"http://graphical.weather.gov/xml/SOAP_server/ndfdXMLserver.php" forHTTPHeaderField:@"Endpoint"];
    [theRequest addValue: @"http://graphical.weather.gov/xml/DWMLgen/wsdl/ndfdXML.wsdl#NDFDgenByDay" forHTTPHeaderField:@"SoapAction"];
    [theRequest addValue:@"rpc" forHTTPHeaderField:@"Style"];
    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
    [theRequest setHTTPMethod:@"POST"];
    [theRequest setHTTPBody: [soapContent dataUsingEncoding:NSUTF8StringEncoding]];

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    if (theConnection)
        receivedData = [NSMutableData data];
    else
        NSLog(@"theConnection is NULL");

soapRequest.xml看起来像

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
    <ns6244:NDFDgenByDay xmlns:ns6244="uri:DWMLgenByDay">
        <latitude xsi:type="xsd:string">38.99</latitude>
        <longitude xsi:type="xsd:string">-77.01</longitude>
        <startDate xsi:type="xsd:string">2012-03-12</startDate>
        <numDays xsi:type="xsd:string">7</numDays>
        <format xsi:type="xsd:string">24 hourly</format>
    </ns6244:NDFDgenByDay>
</SOAP-ENV:Body>

我确信soapRequest.xml是正确的,因为它在NDFD网站上发布为sample code。但是,我不知道设置HTTP标头的正确方法。如果有人熟悉这个或知道问题是什么,请帮助我。

1 个答案:

答案 0 :(得分:1)

我通过将soapRequest.xml更改为ff:

来使您的程序正常工作
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body>
        <ns6244:NDFDgenByDay xmlns:ns6244="uri:DWMLgenByDay">
            <latitude xsi:type="xsd:decimal">38.99</latitude>
            <longitude xsi:type="xsd:decimal">-77.01</longitude>
            <startDate xsi:type="xsd:date">2012-08-07</startDate>
            <numDays xsi:type="xsd:integer">7</numDays>
            <Unit xsi:type="xsd:string">m</Unit>
            <format xsi:type="xsd:string">24 hourly</format>
        </ns6244:NDFDgenByDay>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>