如何在ios中使用带有身份验证的SOAP服务?

时间:2014-03-04 09:09:06

标签: ios iphone objective-c xml soap

我正在开发一个应用程序,我需要使用WSDL格式的SOAP webservice。

但它需要在使用服务之前进行身份验证。

我不知道如何使用SOAP服务进行身份验证。

我试过这段代码: -

-(void)callAPI
{

        NSString *soapMessage = [NSString stringWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"                                                           "<wsdl:definitions name=\"hubbuchUser\"                                targetNamespace=\"http://XXX.yy.com/soap/hubbuch/\"                          xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\"                              xmlns:tns=\"http://XXX.yy.com/soap/hubbuch/\"                                                 xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"                                xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\">\n"                                     "<wsdl:types></wsdl:types>\n"                                             "<wsdl:binding name=\"hubbuchUserBinding\" type=\"tns:hubbuchUserPortType\">"
                                 "<soap:binding style=\"rpc\"   transport=\"http://schemas.xmlsoap.org/soap/http\" />"
                                 "<wsdl:operation name=\"isValidDVN\">"
                                 "<soap:operation soapAction=\"http://XXX.yy.com/soap/hubbuch/isValidDVN\" />"
                                 "<wsdl:input>"
                                 "<soap:body use=\"literal\" namespace=\"http://XXX.yy.com/soap/hubbuch/\" />"
                                 "</wsdl:input>"
                                 "<wsdl:output>"
                                 "<soap:body use=\"literal\" namespace=\"http://XXX.yy.com/soap/hubbuch/\" />"
                                 "</wsdl:output>"
                                 "</wsdl:operation>"
                                 "</wsdl:binding>"

            "</wsdl:definitions>"];

        NSURL *url = [NSURL URLWithString:@"http://XXX.yy.com/soap/hubbuch/hubbuchUser.wsdl"];
        NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
        NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

        [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
        [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];
        [theRequest setHTTPMethod:@"POST"];
        [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

        NSURLConnection *theConnection =[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

        if( theConnection )
        {
            webData = [NSMutableData data] ;
            NSLog(@"Problem");
        }
        else
        {
            NSLog(@"theConnection is NULL");
        }

}

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
    {
        [webData setLength: 0]; 
    }

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [webData appendData:data];

    }

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        NSLog(@"ERROR with theConenction");

    }

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection
    {

     NSLog(@"DONE. Received Bytes: %d", [webData length]);
        NSString *theXML = [[NSString alloc] initWithBytes:                  
          [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
        NSLog(@"xml is- %@ ", theXML);

        NSData *myData = [theXML dataUsingEncoding:NSUTF8StringEncoding];

        NSXMLParser *xmlParser = [[NSXMLParser alloc] initWithData:myData];

        // Don't forget to set the delegate!
        xmlParser.delegate = self;

        // Run the parser
        [xmlParser parse];

    }

    -(void) parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName  namespaceURI:(NSString *)namespaceURI qualifiedName:
    (NSString *)qName attributes:(NSDictionary *)attributeDict
    {
       //curDescription = [[NSString alloc]init];
        NSLog(@"attributeDict : %@",attributeDict);
        NSLog(@"qName1 : %@",qName);
        NSLog(@"elementName1 : %@",elementName);

    }

    - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
    {
        //curDescription = string;
        NSLog(@"string : %@",string);
    }

    - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName
      namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
    {
        //NSLog(@"3 : %@",curDescription);


        NSLog(@"qName2 : %@",qName);
        NSLog(@"elementName2 : %@",elementName);

    }

我该怎么做?

感谢。

1 个答案:

答案 0 :(得分:0)

试试这个代码......

-(NSDictionary*) getTimeCardActivityDetailService:(NSString *)userName:(NSString *)passWord
 {
   NSString *dataBase = [self getDatabaseNameFromPlist];

 strxml = @"<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xyt=\"urn:xytechsystems.com/XytechAPILite\" xmlns:xyt1=\"http://schemas.datacontract.org/2004/07/Xytech.MP.API\" xmlns:arr=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\">";

strxml = [strxml stringByAppendingString:@"<soapenv:Header/>"];
strxml = [strxml stringByAppendingString:@"<soapenv:Body>"];
strxml = [strxml stringByAppendingString:@"<xyt:GetItemsiOS>"];

strxml = [strxml stringByAppendingString:@"<xyt:credentials>"];

strxml = [strxml stringByAppendingString:@"<xyt1:DBName>"];
strxml = [strxml stringByAppendingString:dataBase];
strxml = [strxml stringByAppendingString:@"</xyt1:DBName>"];

strxml = [strxml stringByAppendingString:@"<xyt1:Password>" ];
strxml = [strxml stringByAppendingString:passWord ];
strxml = [strxml stringByAppendingString:@"</xyt1:Password>" ];

strxml = [strxml stringByAppendingString:@"<xyt1:UserID>" ];
strxml = [strxml stringByAppendingString:userName ];
strxml = [strxml stringByAppendingString:@"</xyt1:UserID>" ];
strxml = [strxml stringByAppendingString:@"</xyt:credentials>"];

strxml = [strxml stringByAppendingString:@"<xyt:className>TimeCardActivities"];
strxml = [strxml stringByAppendingString:@"</xyt:className>"];

strxml = [strxml stringByAppendingString:@"<xyt:includeMetaData>"];
strxml = [strxml stringByAppendingString:@"FirstRowOnly"];
strxml = [strxml stringByAppendingString:@"</xyt:includeMetaData>"];
strxml = [strxml stringByAppendingString:@"<xyt:criteria>"];
strxml = [strxml stringByAppendingString:@"<xyt1:Criteria i:nil=\"true\"/>"];
strxml = [strxml stringByAppendingString:@"<xyt1:MaxRows>0</xyt1:MaxRows>"];

strxml = [strxml stringByAppendingString:@"</xyt:criteria>"];
strxml = [strxml stringByAppendingString:@"</xyt:GetItemsiOS>" ];
strxml = [strxml stringByAppendingString:@"</soapenv:Body>" ];
strxml = [strxml stringByAppendingString:@"</soapenv:Envelope>" ];


NSString * xmlResponse = [self callWSForGettingActivityDetail:strxml];
NSDictionary *activityDetailDic=  [self loadDataFromXMLOfActivityDetail:xmlResponse];

使用nsstring变量,附加带有db name / password(credentials)的标记