這是你可以調用SOAP Web服務:
在您的.h文件中聲明:
NSMutableData *webPortFolio;
NSMutableString *soapResultsPortFolio;
NSURLConnection *conn;
//---xml parsing---
NSXMLParser *xmlParserPortFolio;
BOOL elementFoundPortFolio;
NSMutableURLRequest *req;
NSString *theXMLPortFolio;
NSString *strSoapMsg;
UIAlertView *alertView;
在您.m文件使用下面的代碼:
-(void)loadPortfolioData
{
strSoapMsg = [[NSString alloc] initWithFormat:
@"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">"
"<soap12:Body>"
"<GetPortfolioList xmlns=\"http://tempuri.org/\">"
"<EmailID>%@</EmailID>"
"<Password>%@</Password>"
"<TradingGameID>%d</TradingGameID>"
"</GetPortfolioList>"
"</soap12:Body>"
"</soap12:Envelope>",gameUserName,gamePassword,gameid];
//---print it to the Debugger Console for verification---
NSLog(@"soapMsg..........%@",strSoapMsg);
NSURL *url = [NSURL URLWithString:@"http://www.abc.sirus/Process/process.asmx"];
req = [NSMutableURLRequest requestWithURL:url];
//---set the headers---
NSString *msgLength = [NSString stringWithFormat:@"%d",[strSoapMsg length]];
[req addValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[req addValue:@"http://tempuri.org/GetPortfolioList" forHTTPHeaderField:@"SOAPAction"];
[req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
//---set the HTTP method and body---
[req setHTTPMethod:@"POST"];
[req setHTTPBody: [strSoapMsg dataUsingEncoding:NSUTF8StringEncoding]];
// [activityIndicator startAnimating];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
if (conn)
{
webPortFolio = [[NSMutableData data] retain];
}
}
而要處理響應,您可以使用以下功能:
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[webPortFolio setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[webPortFolio appendData:data];
}
-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error
{
NSLog(@"error...................%@",[error description]);
[webPortFolio release];
[connection release];
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{
//Check the request and returns the response.
NSLog(@"DONE. Received Bytes: %d", [webPortFolio length]);
theXMLPortFolio = [[NSString alloc]
initWithBytes: [webPortFolio mutableBytes]
length:[webPortFolio length]
encoding:NSUTF8StringEncoding];
//---shows the XML---
NSLog(@"shows the XML %@",theXMLPortFolio);
[theXMLPortFolio release];
if(xmlParserPortFolio)
{
[xmlParserPortFolio release];
}
xmlParserPortFolio = [[NSXMLParser alloc] initWithData: webPortFolio];
[xmlParserPortFolio setDelegate: self];
[xmlParserPortFolio setShouldResolveExternalEntities:YES];
[xmlParserPortFolio parse];
[webPortFolio release];
[connection release];
}
//---when the start of an element is found---
-(void) parser:(NSXMLParser *) parser
didStartElement:(NSString *) elementName
namespaceURI:(NSString *) namespaceURI
qualifiedName:(NSString *) qName
attributes:(NSDictionary *) attributeDict
{
if([elementName isEqualToString:@"GetPortfolioListResult"])
{
if (!soapResultsPortFolio)
{
soapResultsPortFolio = [[NSMutableString alloc] init];
}
elementFoundPortFolio = TRUE;
NSLog(@"Registration...%@",soapResultsPortFolio);
}
else if([elementName isEqualToString:@"your_tag_name"])
{
elementFoundPortFolio = TRUE;
}
else if([elementName isEqualToString:@"your_tag_name"])
{
elementFoundPortFolio = TRUE;
}
else if([elementName isEqualToString:@"your_tag_name"])
{
elementFoundPortFolio = TRUE;
}
}
-(void)parser:(NSXMLParser *) parser foundCharacters:(NSString *)string
{
if (elementFoundPortFolio)
{
[soapResultsPortFolio appendString: string];
}
}
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError
{
NSLog(@"Parser error %@ ",[parseError description]);
}
//---when the end of element is found---
-(void)parser:(NSXMLParser *)parser
didEndElement:(NSString *)elementName
namespaceURI:(NSString *)namespaceURI
qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"GetPortfolioListResult"])
{
NSLog(@"display the soap results%@",soapResultsPortFolio);
}
else if([elementName isEqualToString:@"your_tag_name"])
{
//Perform required action
}
else if([elementName isEqualToString:@"your_tag_name"])
{
//Perform required action
}
else if([elementName isEqualToString:@"your_tag_name"])
{
//Perform required action
}
[soapResultsPortFolio setString:@""];
elementFoundPortFolio = FALSE;
}
什麼gameUserName和gamePassword這裏。 –
@DeepikaLalra這些是即時通信作爲我的XML Web服務參數的字符串。 – Rushi