您的位置:首页 > 理论基础 > 计算机网络

<iOS>网络编程(二)SOAP, WSDL, Web Service

2012-12-14 16:24 435 查看
上篇文章讲到了,使用网上一个工具WSDL2ObjC.app来把已有的 wsdl转化为ios可使用的代码, 然后通过这部分代码进行soap的请求与获取响应。

但是上面这种方式的劣势也不言自明, 所以下面将用最原始的方式来做同样的事情。

这里首先需要感谢的提供这个网站供使用的网址:http://www.ripedevelopment.com/webservices/LocalTime.asmx

好, 我们开始吧!

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:");

if( [elementNameisEqualToString:@"LocalTimeByZipCodeResult"])
{

recordResults =FALSE;

NSLog(@"receivedResult timezone=%@",soapResults);
[soapResultsrelease];

soapResults = nil;

NSLog(@"hoursOffset result");
}
}
- (void)parserDidStartDocument:(NSXMLParser *)parser{

NSLog(@"-------------------start--------------");
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{

NSLog(@"-------------------end--------------");
}

我们再查一下,这个web service返回的数据是怎么样的?如下:
HTTP/1.0 200 OK

Content-Type: text/xml; charset=utf-8

Content-Length: 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>

<LocalTimeByZipCodeResponse xmlns="http://www.ripedev.com/">

<LocalTimeByZipCodeResult>string</LocalTimeByZipCodeResult>

</LocalTimeByZipCodeResponse>

</soap:Body>

</soap:Envelope>

// 从上面加粗, 加绿色的文字,我们可以知道, 我们要找的内容在这里。所以这也是我们为什么在Parse的解析中要使用这个LocalTimeByZipCodeResult的原因

好吧, 运行看看吧, 数据已经返回了。
(再PS一下, 上面是直接取数的, 真实的环境中, 应该采用线程的方式,以免影响界面主线程。)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: