gpt4 book ai didi

ios - iOS 应用程序中的 SOAP

转载 作者:行者123 更新时间:2023-11-29 01:55:58 24 4
gpt4 key购买 nike

我正在尝试开发一个使用来自 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

enter image description here

enter image description here

更新在 SOA 客户端检查时我得到以下 header

<?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>

enter image description here

关于ios - iOS 应用程序中的 SOAP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30840687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com