Moving from NSURLConnection to NSURLSession for SOAP POST in Swift
In case anyone else has the same issue, I sorted this out. The issue was I was not using the completionHandler in the didReceiveChallenge and didReceiveResponse delegates
SOAP request using NSURLSession Objective C or SWIFT
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 :
- (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 track multiple get requests?
So,
what worked best for me so far is using the NSDictionary approach. That allows the NSURLSession delegates to get a hold of the right data task and handle multiple requests at once.
I am not sure if this is the best approach, but my app is now running lots of requests and things seems to work fine.
NSURLSession track multiple get requests?
So,
what worked best for me so far is using the NSDictionary approach. That allows the NSURLSession delegates to get a hold of the right data task and handle multiple requests at once.
I am not sure if this is the best approach, but my app is now running lots of requests and things seems to work fine.
Related Topics
Why I Can't Access My 3Rd Level Coredata Data in Swift
How to Determine Whether a Double Is an Integer
How to Fix Dylib with Invalid Signature
Communication Error When Uploading 90 Mb IPA File with Swift to Appstore
How to Populate Table Rows, Using a [String] Array Sent from iPhone by Watch Connectivity
Cocos2D Fcm Push Notification Not Working
Hit Fatal Error: Unexpectedly Found Nil While Unwrapping an Optional Value (Lldb)
How to Get and Save The Mixed of Multiple Audios in to Single Audio in Swift
Uicollectionview Selected Cells Issue
Choppy Animation Swiftui Nested Views
Overlaying Image on Video Reduces Video Resolution
Is It a Good Way to Access Instance Variable with Self? If I Use a Lot
Gmsmarker Not Appearing on Map
Implementing a Custom Viewmodifier Where Output Is Conditional on Concrete View Type (Swiftui)
Nstimer Scheduledtimerwithtimeinterval and Target Is "Class Level Function"