objective-c - iOS - 如何发出 SOAP 请求并接收关注响应

标签 objective-c soap nsurlconnection

我知道网上有很多关于“如何在 iOS 中使用 SOAP”的内容,但我仍然未能遵循 SAOP 请求和响应。非常感谢帮助。 我使用简单的 NSURLConnection 进行请求和响应 SOAP 请求

POST ???.asmx HTTP/1.1
Host: ???
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/GetMessages"

<?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/">
  <soap:Body>
    <GetMessages xmlns="http://tempuri.org/">
      <GroupName>string</GroupName>
      <Date>dateTime</Date>
    </GetMessages>
  </soap:Body>
</soap:Envelope>

SOAP 响应

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?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/">
  <soap:Body>
    <GetMessagesResponse xmlns="http://tempuri.org/">
      <GetMessagesResult>xml</GetMessagesResult>
    </GetMessagesResponse>
  </soap:Body>
</soap:Envelope>

这是我为发送请求而写的代码..

NSString *soapMessage = [NSString stringWithFormat:
                         @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
                         "<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/\">\n"
                         "<soap:Body>\n"
                         "<GetMessages xmlns=\"http://tempuri.org/\">\n"
                         "<GroupName>%@</GroupName>\n"
                         "<Date>%@</Date>\n"
                         "</GetMessages>\n"
                         "</soap:Body>\n"
                         "</soap:Envelope>\n"
                         , txtfield1.text
                         , textfield2.text
                         ];
NSLog(@"soapMessage: \n%@",soapMessage);

NSURL *url = [NSURL URLWithString:@"???.asmx"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[theRequest addValue: @"http://tempuri.org/GetMessages" forHTTPHeaderField:@"SOAPAction"];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

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

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

我在 connectionDidFinishLoading 中收到此错误

  <soap:Fault>
     <faultcode>soap:Server</faultcode>
     <faultstring>Server was unable to process request. ---&gt; SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.</faultstring>
     <detail />
  </soap:Fault>

我提到了 this link & used the code in this link & modified according to my convenience to make it worked

我的问题是如何发送请求和接收关注回复

提前致谢

最佳答案

问题似乎与 soap 消息的发送/接收完全无关,而是与您作为 dateTime 发送的值有关——它没有正确的格式。

=> 服务器无法处理请求。 ---> SqlDateTime 溢出。必须介于 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间。

使用 NSDateFormatter 以请求的格式写入日期字符串

关于objective-c - iOS - 如何发出 SOAP 请求并接收关注响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14274492/

相关文章:

ios - 出现通知横幅时设备方向不正确

ios - 自定义 uitableviewcell 标签隐藏/显示

Python - 分割文件

ios - 从 NSURLConnection 收集数据

objective-c - Xcode4.5 汇编程序无法编译 Xcode4.4 完美处理的文件

java - 基于中间件的 HTTP/JMS 混合 SOAP 的请求-回复模型

c# - 添加服务引用 : Ordering of Serialization Fields

nsurlconnection - 取消 NSURLConnection 以 sendAsynchronousRequest :queue:completionHandler:? 开始

ios - 错误域=NSURLErrorDomain 代码=-1009 "The Internet connection appears to be offline."

iphone - 我需要释放返回的 NSError 对象吗?