1。 我们打开上面这个网址,左边, 我们可以看到两个同名方法, LocalTimeByZipCode, 下面一个是用来binding LocalTimeSoap12的, 先不管它, 打开上面这个。
打开后, 可以看到这个方法的Overview描述信息,Input Parameters, Output Parameters.
然后点开Test Form, 输入一个zipcode可以进行在线测试,如输入12345. 这个测试可以表明,这个web service目前是可以提供服务的。
再点击, Message Layout,
好的, 我们可以看到有Soap, HTTP Get, HTTP Post三种方式的使用方法。 这里我们侧重于讲SOAP方式。
SOAP部分中, 上面框中显示的是发起请求时, 需要提交的SOAP内容包, 下面显示提正常回复的SOAP信息包。
这就是我们要看的内容:
POST /webservices/LocalTime.asmx
SOAPAction: http://www.ripedev.com/LocalTimeByZipCode
Content-Type: text/xml; charset=utf-8
Content-Length: string
Host: string
<?xml version="1.0" encoding="utf-16"?>
<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>
<LocalTimeByZipCode xmlns="http://www.ripedev.com/">
<ZipCode>string</ZipCode>
</LocalTimeByZipCode>
</soap:Body>
</soap:Envelope>
然后我们创建我们的xcode项目, 和先前的一样, 建一个按钮,再一个textView来进行显示即可。
这是我按钮的事件:
- (void)startRequestToWsdl2:(id)sender {
NSString *soapMessage = [NSString stringWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
"<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/\">\n"
"<soap:Body>\n"
"<LocalTimeByZipCode xmlns=\"http://www.ripedev.com/\">"
"<ZipCode>12345</ZipCode>\n"
"</LocalTimeByZipCode>\n"
"</soap:Body>\n"
"</soap:Envelope>\n"];
// 顺便插一句, SOAP中,Header部分可有可无 , Fault部分可有可无, 但Body和Envelope必须有.
// 上面这部分几乎按SOAP给的格式就行了。下面这个地址是来自于哪里呢, 就来自于这个网址,即上面我们要感谢的这个网址:http://www.ripedevelopment.com/webservices/LocalTime.asmx
NSString *address =@"http://www.ripedevelopment.com/webservices/LocalTime.asmx";
NSURL* url = [NSURLURLWithString:address];
NSMutableURLRequest *theRequest = [NSMutableURLRequestrequestWithURL:url];
// 然后就是text/xml, 和content-Length必须有。
[theRequest addValue: @"text/xml; charset=utf-8"forHTTPHeaderField:@"Content-Type"];
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
// 下面这行, 后面SOAPAction是规范, 而下面这个网址来自哪里呢,来自于上面加红加粗的部分。
[theRequest addValue: @"http://www.ripedev.com/LocalTimeByZipCode"forHTTPHeaderField:@"SOAPAction"];
[theRequestsetHTTPMethod:@"POST"];
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSURLConnection *theConnection = [[NSURLConnectionalloc] initWithRequest:theRequestdelegate:self];
if(theConnection) {
webData = [[NSMutableData data] retain];
} else {
NSLog(@"theConnection is NULL");
}
好的, 上面已经把请求给发起了, 下面我们接收数据,并进行XMLParse解析。
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webDatasetLength: 0];
NSLog(@"connection: didReceiveResponse:1");
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webDataappendData:data];
NSLog(@"connection: didReceiveData:2");
}
//如果电脑没有连接网络,则出现此信息(不是网络服务器不通)
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR with theConenction");
[connection release];
[webDatarelease];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"3 DONE. Received Bytes: %d", [webDatalength]);
NSString *theXML = [[NSStringalloc] initWithBytes: [webDatamutableBytes] length:[webDatalength] encoding:NSUTF8StringEncoding];
NSLog(@"received data=%@", theXML);
[theXML release];
//重新加載xmlParser
if(xmlParser) {
[xmlParserrelease];
}
xmlParser = [[NSXMLParseralloc] initWithData:webData];
[xmlParsersetDelegate: self];
[xmlParsersetShouldResolveExternalEntities:YES];
[xmlParser parse];
[connection release];
}
// 上面完成了数据的接收, 下面进行NSXMLParser 的解析。
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName
attributes: (NSDictionary *)attributeDict
{
NSLog(@"4 parser didStarElemen: namespaceURI: attributes:%@", elementName);
if( [elementNameisEqualToString:@"LocalTimeByZipCodeResult"]) {
if(!soapResults) {
soapResults = [[NSMutableString alloc] init];
}
recordResults =YES;
}
}
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
NSLog(@"5 parser: foundCharacters:");
if(recordResults) {
[soapResultsappendString: string];
}
}
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"6 parser: didEndElement:");