我正在尝试开发一个使用SOAP服务中的数据的应用程序。是我第一次使用SOAP服务。阅读了几本教程之后,我认为我可以以正确的方式将请求设置为服务,但是我总是在响应中出错。怎么了 ?!
NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<env:Envelope xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ns1=\"urn:Lottery.Intf-ILottery\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:enc=\"http://www.w3.org/2003/05/soap-encoding\">\n"
"<env:Body>\n"
"<ns1:GetUpcomingLotteries env:encodingStyle=\"http://www.w3.org/2003/05/soap-encoding\">\n"
"<EntityID xsi:type=\"xsd:int\">2</EntityID>\n"
"<Password xsi:type=\"xsd:string\">Smart@Winners</Password>\n"
"<SortBy xsi:type=\"xsd:int\">0</SortBy>\n"
"<limit xsi:type=\"xsd:int\">0</limit>\n"
"</ns1:GetUpcomingLotteries>\n"
"</env:Body>\n"
"</env:Envelope>"];
NSURL *sRequestURL = [NSURL URLWithString:@"xxxxxxxxx"];
NSMutableURLRequest *myRequest = [NSMutableURLRequest requestWithURL:sRequestURL];
NSString *sMessageLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[myRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[myRequest addValue: @"urn:Lottery.Intf-ILottery/GetUpcomingLotteries" forHTTPHeaderField:@"SOAPAction"];
[myRequest addValue: sMessageLength forHTTPHeaderField:@"Content-Length"];
[myRequest setHTTPMethod:@"POST"];
[myRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:myRequest returningResponse:&response error:&error];
NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
最佳答案
下载ATTACHED PROJECT HERE
//在.h文件中
#import "TBXML.h"
@property(retain,nonatomic)NSString *xmlString;
@property(nonatomic,retain)NSMutableData *webResponseData;
//在.m文件中
-(void)method_GetUpcomingLotteries
{
NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
"<env:Envelope xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:ns1=\"urn:Lottery.Intf-ILottery\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:enc=\"http://www.w3.org/2003/05/soap-encoding\">\n"
"<env:Body>\n"
"<ns1:GetUpcomingLotteries env:encodingStyle=\"http://www.w3.org/2003/05/soap-encoding\">\n"
"<EntityID xsi:type=\"xsd:int\">2</EntityID>\n"
"<Password xsi:type=\"xsd:string\">Smart@Winners</Password>\n"
"<SortBy xsi:type=\"xsd:int\">0</SortBy>\n"
"<limit xsi:type=\"xsd:int\">0</limit>\n"
"</ns1:GetUpcomingLotteries>\n"
"</env:Body>\n"
"</env:Envelope>"];
NSURL *sRequestURL = [NSURL URLWithString:@"xxxxxxxxx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:sRequestURL];
NSString *sMessageLength = [NSString stringWithFormat:@"%d", [soapMessage length]];
[request addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request addValue: @"urn:Lottery.Intf-ILottery/GetUpcomingLotteries" forHTTPHeaderField:@"SOAPAction"];
[request addValue: sMessageLength forHTTPHeaderField:@"Content-Length"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:[soapMessage dataUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"soapMessage===\n %@",soapMessage);
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if( theConnection ) {
NSLog(@"hiii");
[self showUpdating];
self.webResponseData = [NSMutableData data];
}else {
NSLog(@"Some error occurred in Connection");
[self showNetworkError];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Received Bytes from server: %lu", (unsigned long)[self.webResponseData length]);
NSString * strXml = [[NSString alloc] initWithBytes: [self.webResponseData mutableBytes] length:[self.webResponseData length] encoding:NSUTF8StringEncoding];
NSLog(@"---- %@" ,strXml);
TBXML *sourceXML = [[TBXML alloc] initWithXMLString:strXml error:nil];
TBXMLElement *rootElement = sourceXML.rootXMLElement;
TBXMLElement *entryElement = [TBXML childElementNamed:@"soap:Body" parentElement:rootElement];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[self.webResponseData setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.webResponseData appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"Some error in your Connection. Please try again.");
[self showNetworkError];
}
或打开FireFox
安装SOA客户端扩展
在那测试您的API
在SOA客户端中检查更新时,我得到以下标头
<?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><GetUpcomingLotteries xmlns="http://tempuri.org/"><EntityID>2</EntityID><Password>Smart@Winners</Password><SortBy>0</SortBy><limit>0</limit></GetUpcomingLotteries></soap:Body></soap:Envelope>
和以下响应(可能是服务器错误)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"/>
<title>500 - Internal server error.</title>
<style type="text/css">
<!--
body{margin:0;font-size:.7em;font-family:Verdana, Arial, Helvetica, sans-serif;background:#EEEEEE;}
fieldset{padding:0 15px 10px 15px;}
h1{font-size:2.4em;margin:0;color:#FFF;}
h2{font-size:1.7em;margin:0;color:#CC0000;}
h3{font-size:1.2em;margin:10px 0 0 0;color:#000000;}
#header{width:96%;margin:0 0 0 0;padding:6px 2% 6px 2%;font-family:"trebuchet MS", Verdana, sans-serif;color:#FFF;
background-color:#555555;}
#content{margin:0 0 0 2%;position:relative;}
.content-container{background:#FFF;width:96%;margin-top:8px;padding:10px;position:relative;}
-->
</style>
</head>
<body>
<div id="header"><h1>Server Error</h1></div>
<div id="content">
<div class="content-container"><fieldset>
<h2>500 - Internal server error.</h2>
<h3>There is a problem with the resource you are looking for, and it cannot be displayed.</h3>
</fieldset></div>
</div>
</body>
</html>
关于ios - iOS应用程序中的SOAP,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30840687/