iOS - 如何制作SOAP请求&收到关注回复

时间:2013-01-11 08:46:43

标签: objective-c soap nsurlconnection

我知道网上有很多关于“如何在iOS中使用SOAP”的东西,但我仍然无法遵循SAOP请求&响应。帮助非常值得赞赏。  我使用简单的NSURLConnection作为请求&响应 SOAP Requst

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

我的问题是如何发送请求&amp;收到关注回复

提前多多感谢

1 个答案:

答案 0 :(得分:2)

问题似乎与肥皂消息的发送/收集无关,与您发送的值dateTime无关 - 它的格式不正确。

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

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

相关问题