Ok, I could never get a web service or WCF service to accept HTTP GET requests. I did manage to figure out how to manually create a soap envelope and call a 2.0 web service. Here's what the service tells me it needs:
HTML Code:
POST /<my service name>/Service1.asmx HTTP/1.1
Host: <my service host>
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://heyWorld.org/HelloWorld"
<?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>
<HelloWorld xmlns="http://heyWorld.org/">
<userName>string</userName>
</HelloWorld>
</soap:Body>
</soap:Envelope>
The above is from the default asp.net web service you get when creating a new project. I have changed the namespace associated with the function calls from tempuri.org, and added a string parameter to the default HelloWorld function that gets created for you.
The following obj-c creates the soap envelope, creates a NSURLRequest with the necessary content-type etc. (check out everything I do with the 'request' variable), and finally connects and sends the request. I put the NSMutableData variable into an NSString just so I could see it from the debugger, but you can also use this for your parser.
Code:
NSMutableString *sRequest = [[NSMutableString alloc]init];
//create soap envelope
[sRequest appendString:@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"];
[sRequest appendString:@"<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/\">"];
[sRequest appendString:@"<soap:Body>"];
[sRequest appendString:@"<HelloWorld xmlns=\"http://heyWorld.org/\">"];
[sRequest appendString:@"<userName>foo</userName>"];
[sRequest appendString:@"</HelloWorld>"];
[sRequest appendString:@"</soap:Body>"];
[sRequest appendString:@"</soap:Envelope>"];
NSURL *myWebserverURL = [NSURL URLWithString:@"http://<my host>/<my project>/Service1.asmx"];
[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[myWebserverURL host]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:myWebserverURL];
[request addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue:@"http://heyWorld.org/HelloWorld" forHTTPHeaderField:@"SOAPAction"];//this is default tempuri.org, I changed mine in the project
NSString *contentLengthStr = [NSString stringWithFormat:@"%d", [sRequest length]];
[request addValue:contentLengthStr forHTTPHeaderField:@"Content-Length"];
// Set the action to Post
[request setHTTPMethod:@"POST"];
// Set the body
[request setHTTPBody:[sRequest dataUsingEncoding:NSUTF8StringEncoding]];
// Create the connection
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSMutableData *myMutableData;
// Check the connection object
if(conn)
{
myMutableData=[[NSMutableData data] retain];
}
// Make this class the delegate so that the other connection events fire here.
[NSURLConnection connectionWithRequest:request delegate:self];
NSError *WSerror;
NSURLResponse *WSresponse;
// Execute the asp.net Service and return the data in an NSMutableData object
myMutableData = [NSURLConnection sendSynchronousRequest:request returningResponse:&WSresponse error:&WSerror];
//convert the mutabledata to an nsstring so I can see it with the debugger
NSString *theXml = [[NSString alloc]initWithBytes:[myMutableData mutableBytes] length:[myMutableData length] encoding:NSUTF8StringEncoding];
Be sure to replace <my host> and <my project> where appropriate (and anything else I have up there that's <my *>), and make sure you are using the namespace you have declared for the service (mine is
http://heyWorld.org/ and my function is HelloWorld.)
Credit for the above discoveries and most of the code:
Tom McCartan