使用 NSURLSession Objective C 或 SWIFT 的 SOAP 请求 [英] SOAP request using NSURLSession Objective C or SWIFT

查看:68
本文介绍了使用 NSURLSession Objective C 或 SWIFT 的 SOAP 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何使用 NSURLSession 执行 SOAP 请求?请查找 SOAP 的请求格式.

How i can perform SOAP request using NSURLSession? Please find request format for SOAP.

 <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pm="http://sampleurl.com/webservices/pm">
 <soapenv:Header/>
 <soapenv:Body>
  <pm:start>
     <username>awdmmappadmin1</username>
     <password>password</password>
     <completedDate>2016-04-25T13:37:34.699Z</completedDate>
     <gameMinutes>2</gameMinutes>
     <gameMoves>14</gameMoves>
     <gameSeconds>33</gameSeconds>
     <gameTimeSec>153</gameTimeSec>
     <guestId>2016</guestId>
  </pm:start>

推荐答案

我使用 Objective C 为 NSURLSession 做了以下实现.效果很好:

I did below implementation for NSURLSession using Objective C. It works fine :

-(void)startSoapRequest{

NSString *soapMessage =
@"<soapenv:Envelopexmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:pm=\"http://appiancorp.com/webservices/pm\">\n<soapenv:Header/>\n<soapenv:Body>\n<pm:start>\n<username>awdmmappadmin1</username>\n<password>password</password>\n<completedDate>2016-04-25T13:37:34.699Z</completedDate>\n<gameMinutes>2</gameMinutes>\n<gameMoves>14</gameMoves>\n<gameSeconds>33</gameSeconds>\n<gameTimeSec>153</gameTimeSec>\n<guestId>2016</guestId>\n</pm:start>\n</soapenv:Body>\n</soapenv:Envelope>";

NSURL *url = [NSURL URLWithString:@"http://sampleurl.com/suite/webservice/processmodel/sample"];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSString *msgLength = [NSString stringWithFormat:@"%lu", (unsigned long)[soapMessage length]];

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

 NSURLSession *soapSession = [NSURLSession sessionWithConfiguration:   [NSURLSessionConfiguration defaultSessionConfiguration] delegate:self delegateQueue:[NSOperationQueue mainQueue]];
NSURLSessionDataTask *dataTask = [soapSession dataTaskWithURL: url];
self.responseData = [[NSMutableData alloc]init];
 [dataTask resume];
}

NSURLSessionTaskDelegate :

NSURLSessionTaskDelegate :

 - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
 {
   //handle data here
   [self.responseData appendData:data];

 }


- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
 {
//Called when the data transfer is complete
//Client side errors are indicated with the error parameter

if (error) {

    NSLog(@"%@ failed: %@", task.originalRequest.URL, error);

}else{

    NSLog(@"DONE. Received Bytes: %lu", (unsigned long)[self.responseData length]);

    NSString *theXML = [[NSString alloc] initWithBytes:
                        [self.responseData bytes] length:[self.responseData length] encoding:NSUTF8StringEncoding];
    NSLog(@"%@",theXML);
 }
}

这篇关于使用 NSURLSession Objective C 或 SWIFT 的 SOAP 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆