web-services - 使用 Objective C 进行 SOAP 查询

标签 web-services cocoa cocoa-touch soap wsdl

我正在尝试使用 SOAP、使用 Objective C 向国家铁路查询系统进行查询。不幸的是,我并没有取得多大进展。我尝试了一些方法,但所发生的只是我得到一组空结果。不幸的是,国家铁路公司不为其系统提供任何支持 - 而且我也从未使用过 SOAP,这一事实使情况变得更加复杂。

架构在这里 - https://realtime.nationalrail.co.uk/ldbws/wsdl.aspx

我已经尝试了很多不同的事情,包括更改主机、API 版本等等 - 但我在黑暗中摸索,漫无目的地试图找到正确的魔法词 - 这不是很有成效。我确信我模糊地处于正确的区域,并且这只是修复请求的问题(下面的代码)。谁能看到我做错了什么吗?

soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
           "<soap:Envelope xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:soap12=\"http://schemas.xmlsoap.org/wsdl/soap12/\" xmlns:tns=\"http://thalesgroup.com/RTTI/2012-01-13/ldb/\" xmlns:ct=\"http://thalesgroup.com/RTTI/2012-01-13/ldb/commontypes\" xmlns:ldbt6=\"http://thalesgroup.com/RTTI/2012-01-13/ldb/types\">"
           "<soap:Header>"
           "<ct:AccessToken>"
           "<ct:TokenValue>MY_TOKEN_GOES_HERE</ct:TokenValue>"
           "</ct:AccessToken>"
           "</soap:Header>"
           "<soap:Body>"
           "<ldbt6:GetDepartureBoardRequest xmlns=\"http://thalesgroup.com/RTTI/2012-01-13/ldb/\">"
           "<ldbt6:numRows>10</ldbt6:numRows>"
           "<ldbt6:crs>LST</ldbt6:crs>"
           "</ldbt6:GetDepartureBoardRequest>"
           "</soap:Body>"
           "</soap:Envelope>"];

NSURL *url = [NSURL URLWithString:@"https://realtime.nationalrail.co.uk/LDBWS/ldb6.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];
[theRequest addValue:@"realtime.nationalrail.co.uk" forHTTPHeaderField:@"Host"];
[theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@"http://thalesgroup.com/RTTI/2012-01-13/ldb/GetDepartureBoard" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue:msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if(connection)
{
    webResponseData = [NSMutableData data] ;
}
else
{
    NSLog(@"Connection is NULL");
}

所有这些都会返回错误:

<div id="header"><h1>Server Error</h1></div>
<div id="content">
 <div class="content-container"><fieldset>
  <h2>401 - Unauthorized: Access is denied due to invalid credentials.</h2>
  <h3>You do not have permission to view this directory or page using the credentials that you supplied.</h3>
 </fieldset></div>
</div>

我使用的 token 是 NationalRail 提供的 token 。是否需要以任何方式引用?

最佳答案

我试图解决同样的问题,并且遇到了从此 link 获得的解决方案。如果您从 OpenLDBWS 注册页面获取访问 token ,您的 token 将仅适用于 OpenLDBWS 端点。

如果您在 OpenLDBWS 出现之前获得 token ,您的 token 将在两者上工作,但是旧的 API 很快就会停用。

NSString *endpoint = @"https://lite.realtime.nationalrail.co.uk/OpenLDBWS/ldb6.asmx";

NSString *message = @"<?xml version=\"1.0\"?>\n"
                     "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://thalesgroup.com/RTTI/2014-02-20/ldb/\" xmlns:ns2=\"http://thalesgroup.com/RTTI/2010-11-01/ldb/commontypes\">\n"
                     " <SOAP-ENV:Header>\n"
                     "  <ns2:AccessToken>\n"
                     "   <ns2:TokenValue>ACCESS-TOKEN-HERE</ns2:TokenValue>\n"
                     "  </ns2:AccessToken>\n"
                     " </SOAP-ENV:Header>\n"
                     " <SOAP-ENV:Body>\n"
                     "  <ns1:GetDepartureBoardRequest>\n"
                     "   <ns1:crs>LST</ns1:crs>\n"
                     "   <ns1:numRows>10</ns1:numRows>\n"
                     "  </ns1:GetDepartureBoardRequest>\n"
                     " </SOAP-ENV:Body>\n"
                     "</SOAP-ENV:Envelope>";


NSURL *url = [NSURL URLWithString:endpoint];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[message length]];

[request addValue:@"thalesgroup.com" forHTTPHeaderField:@"Host"];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[request addValue: @"http://thalesgroup.com/RTTI/2012-01-13/ldb/GetDepartureBoard" forHTTPHeaderField:@"SOAPAction"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: [message dataUsingEncoding:NSUTF8StringEncoding]];

关于web-services - 使用 Objective C 进行 SOAP 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27358885/

相关文章:

ios - 方法泄漏中的对象

web-services - REST 中的表征状态是什么意思?

c# - iisexpress : listen on external interface without modifying config file:

java.util.List 是一个接口(interface),JAXB 不能处理接口(interface)

java - Java Axis2 WebService 中的文件路径问题

objective-c - 使用 isEqualToString 时访问错误

cocoa - 如何跨 nib 文件将菜单项连接到 NSApplication 委托(delegate)中定义的自定义操作?

objective-c - 将字符转换为 cocoa 或碳中的键代码

cocoa :CATransaction 和 NSAnimationContext 有什么不同

iOS 特殊字符,奇怪的问题