Looking at the help document on your server, it suggested an example request:
<?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>
<Customers xmlns="http://tempuri.org/" />
</soap:Body>
</soap:Envelope>
So, I put that example request in a text file, and initiated the request via NSURLConnection
like so:
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *filename = [[NSBundle mainBundle] pathForResource:@"soap1-1example" ofType:@"xml"];
NSData *requestBodyData = [NSData dataWithContentsOfFile:filename];
request.HTTPBody = requestBodyData;
[request setHTTPMethod:@"POST"];
[request addValue:@"http://tempuri.org/Customers" forHTTPHeaderField:@"SOAPAction"];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[NSURLConnection connectionWithRequest:request delegate:self];
Note, I'm not calling start
, because as the documentation says unless you use "initWithRequest:delegate:startImmediately:
method and provide NO
for the startImmediately
parameter," it will start for you automatically. Your original code is, in effect, starting the connection twice, which could be problematic.
Anyway, using my revised code with the above request, it worked fine, using both NSURLConnection
as well as with AFNetworking
. The response I got (prettified) was:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<CustomersResponse xmlns="http://tempuri.org/">
<CustomersResult>
<Customer>
<FirstName>Mohammad</FirstName>
<LastName>Azam</LastName>
</Customer>
<Customer>
<FirstName>John</FirstName>
<LastName>Doe</LastName>
</Customer>
</CustomersResult>
</CustomersResponse>
</soap:Body>
</soap:Envelope>
If it's still not working for you, perhaps you can share the particulars of the request you're submitting.
By the way, since nothing here necessitates the delegate methods (e.g. you're not streaming, you're not doing any authentication, etc.), in you can eliminate all of the NSURLConnectionDataDelegate
methods (didReceiveResponse
, didReceiveData
, etc.), and just use NSURLConnection
class method, sendAsynchronousRequest
(in iOS 5 and later):
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSString *filename = [[NSBundle mainBundle] pathForResource:@"soap1-1example" ofType:@"xml"];
NSData *requestBodyData = [NSData dataWithContentsOfFile:filename];
NSAssert(requestBodyData, @"requestBodyData is nil!");
request.HTTPBody = requestBodyData;
[request setHTTPMethod:@"POST"];
[request addValue:@"http://tempuri.org/Customers" forHTTPHeaderField:@"SOAPAction"];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
if (!self.networkQueue)
{
self.networkQueue = [[NSOperationQueue alloc] init];
self.networkQueue.name = @"com.yourdomain.yourapp.networkQueue";
}
[NSURLConnection sendAsynchronousRequest:request
queue:self.networkQueue
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if (!error)
{
NSString *string = [[NSString alloc] initWithData:data
encoding:NSUTF8StringEncoding];
NSLog(@"%s: data=%@", __FUNCTION__, string);
}
else
{
NSLog(@"%s: error=%@", __FUNCTION__, error);
}
}];