我正在尝试使用 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 ""</detail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>
此错误对象为“格式”值,但如下所示,我的 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 网站上。但是,我不知道设置 HTTP 标头的正确方法。如果有人熟悉这个或知道问题是什么,请帮助我。