封装SOAP请求
#define AW_APP_AWWAPIURL @"http://..." /******************************************************************************** *params: *soapUrl:请求的外部web service的地址及function *requestStr:发送请求的字符串,xml的SOAP格式 **********************************************************************************/ - (NSURLRequest*) CreateSoapRequest : (NSString*) soapUrl : (NSString*) requestStr { NSString* urlPath = [[NSString alloc] initWithFormat:@"%@%@", AW_APP_AWWAPIURL, soapUrl]; NSURL* url = [[NSURL alloc] initWithString:urlPath]; NSLog(requestStr); NSData* requestData = [requestStr dataUsingEncoding: NSASCIIStringEncoding]; NSString* requestDataLengthString = [[NSString alloc] initWithFormat:@"%d", [requestData length]]; NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:url]; [request setHTTPMethod:@"POST"]; [request setHTTPBody:requestData]; [request setValue:@"application/soap+xml" forHTTPHeaderField:@"Content-Type"]; [request setValue:requestDataLengthString forHTTPHeaderField:@"Content-Length"]; [request setTimeoutInterval:30.0]; [urlPath release]; [url release]; [requestDataLengthString release]; //[request release]; return request; }
XML的SOAP传输格式
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing"> <s:Header> <a:Action s:mustUnderstand="1">http://tempuri.org/IAWWAPI/SendEmailToAccount</a:Action> <a:MessageID>urn:uuid:64babe05-7058-43d6-97ce-797fce6a02ec</a:MessageID> <a:ReplyTo> <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address> </a:ReplyTo> <a:To s:mustUnderstand="1">http://www.anglerweb.com:8000/awwapi.svc</a:To> </s:Header> <s:Body> <SendEmailToAccount xmlns="http://tempuri.org/"> <intAccountId>%d</intAccountId> <strFrom>%@</strFrom> <strSubject>%@</strSubject> <strMessage>%@</strMessage> <pKey>254</pKey> </SendEmailToAccount> </s:Body> </s:Envelope>