首先给大家普及一下soap是干什么用的,其实soap是一个访问协议,用来像webservice传输xml格式数据,soap请求消息的格式如下:
<?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/\" xmlns:标识名=命名空间>
<soap:Body>
<标识名:方法名><paramString>参数</paramString></标识名:方法名>
</soap:Body>
</soap:Envelope>
(标识名可以自己定义:比如name等)
好了以上就是soap消息的标准格式,用它就可以进行下边的数据请求了:
//请求发送到的路径
NSURL *url = [NSURLURLWithString:URL];
NSMutableURLRequest *theRequest = [NSMutableURLRequestrequestWithURL:url];
NSString *msgLength = [NSStringstringWithFormat:@"%d", [soap消息 length]];
//以下对请求信息添加属性前四句是必有的,第五句是soap信息。
[theRequest addValue:@"text/xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"];
[theRequest addValue:@""forHTTPHeaderField:@"SOAPAction"];
[theRequestaddValue: msgLengthforHTTPHeaderField:@"Content-Length"];
[theRequestsetHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soap消息 dataUsingEncoding:NSUTF8StringEncoding]];
//请求
NSURLConnection *theConnection = [[NSURLConnectionalloc]initWithRequest:theRequestdelegate:self];
//如果连接已经建好,则初始化data
if( theConnection )
{
_webData = [[NSMutableDatadata]retain];
}
else
{
NSLog(@"theConnection is NULL");
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
//注意这里要把你请求回来的数据转换成UTF8
NSString* response = [[NSStringalloc]initWithData:_webDataencoding:NSUTF8StringEncoding];
NSLog(@"==========%@",response);
[responserelease];
[connection release];
NSData *xmlData = [returnStringdataUsingEncoding:NSUTF8StringEncoding];
_doc = [[DDXMLDocumentalloc]initWithData:xmlDataoptions:0error:nil];
进行解析数据..........
}