ios Web 服务登录返回空的 xml 响应

Posted

技术标签:

【中文标题】ios Web 服务登录返回空的 xml 响应【英文标题】:ios web service login returns empty xml response 【发布时间】:2013-06-13 19:24:06 【问题描述】:

我是一个 ios 菜鸟。我正在开发一个使用肥皂网络服务进行登录操作的 iphone 应用程序。 我读了很多类似的问题,但没有一个对我有帮助.. 我复制粘贴了soap官方代码示例,只更改了url和请求的xml,以便从两个文本视图中获取用户名和密码。连接正常,我有 http 错误 200(不是真正的错误..我知道,所以连接正常)但返回的 webData 对象长度为 0 字节,所以我有一个空的 xml 响应..

这是我的代码:

- (IBAction)actBtnLogin:(id)sender 

    recordResults = NO;

    NSString *soapMessage = [NSString stringWithFormat:
                             @"<SOAP-ENV:Envelope "
                             "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" "
                             "xmlns:ns1=\"urn:dbmanager\" "
                             "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
                             "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                             "xmlns:ns2=\"http://xml.apache.org/xml-soap\" "
                             "xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" "
                             "SOAP-ENV:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"> "
                             "<SOAP-ENV:Body> "
                             "<ns1:camiop> "
                             "<strOp xsi:type=\"xsd:string\">login</strOp> "
                             "<strTessera xsi:type=\"xsd:string\"/> "
                             "<anyParametri xsi:type=\"ns2:Map\"> "
                             "<item> "
                             "<key xsi:type=\"xsd:string\">CRM_USERNAME</key> "
                             "<value xsi:type=\"xsd:string\">%@</value> "
                             "</item> "
                             "<item> "
                             "<key xsi:type=\"xsd:string\">CRM_PASSWORD</key> "
                             "<value xsi:type=\"xsd:string\">%@</value> "
                             "</item> "
                             "<item> "
                             "<key xsi:type=\"xsd:string\">CRM_SITEAPP</key> "
                             "<value xsi:type=\"xsd:int\">2</value> "
                             "</item> "
                             "</anyParametri> "
                             "</ns1:camiop> "
                             "</SOAP-ENV:Body> "
                             "</SOAP-ENV:Envelope> ", _outTxtuser.text, _outTxtpass.text];


    NSLog(@"REQUEST = %@", soapMessage);


    NSURL *url = [NSURL URLWithString:@"http://srvb.mysite.com/dbmgr.class.php"];
    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

    [theRequest addValue:@"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [theRequest addValue:@"srvbop" forHTTPHeaderField:@"SOAPAction"];
    [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] retain];
        NSLog(@"theConnection OK");

    
    else
    
        NSLog(@"theConnection is null");
    




-(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response

    [_webData setLength:0];
    NSHTTPURLResponse * httpResponse;

    httpResponse = (NSHTTPURLResponse *) response;

    NSLog(@"HTTP error %zd", (ssize_t) httpResponse.statusCode);



-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData*)data

    [_webData appendData:data];
    NSLog(@"DID RECEIVE DATA");



-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError*)error

    NSLog(@"error with the connection");


-(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 %@",theXML);

    NSString *theXML = [[NSString alloc] initWithData: _webData encoding:NSUTF8StringEncoding];

    _webData = nil;

    NSLog(@"Response: %@", theXML);
    [_outLabelrich setText:[NSString stringWithFormat:@"Response: %@", theXML]];

    /*xmlParser = [[NSXMLParser alloc] initWithData:webData];
    [xmlParser setDelegate:self];
    [xmlParser setShouldResolveExternalEntities:YES];

    [xmlParser parse];
     */


- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict




    if([elementName isEqualToString:@"Symbol"] || [elementName isEqualToString:@"Last"] || [elementName isEqualToString:@"Time"] )
    
        if(!_soapResults)
        
            _soapResults = [[NSMutableString alloc]init];
        
        recordResults = YES;
    


- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string

    if(recordResults)
    
        [_soapResults appendString:string];
    



- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName

    if([elementName isEqualToString:@"Symbol"] || [elementName isEqualToString:@"Last"] || [elementName isEqualToString:@"Time"] )
    
        recordResults = NO;
        NSLog(@"%@", _soapResults);
        _soapResults = nil;
    




- (void)didReceiveMemoryWarning 
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.


- (void)viewDidUnload 
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;


-(BOOL) textFieldShouldReturn:(UITextField *)textField

    [textField resignFirstResponder];
    return YES;


- (IBAction)backgroundClick:(id)sender 
    [_outTxtuser resignFirstResponder];
    [_outTxtpass resignFirstResponder];

感谢您的帮助

【问题讨论】:

【参考方案1】:

很抱歉没有直接回答您的问题,但是当有许多开发良好且经过高度测试的库可以为您处理繁琐的工作时,我强烈建议您不要编写自定义网络/解析代码。查看AFNetworking,它将为您处理连接和 XML 解析的繁琐工作,同时使用块的便利性。

例如,这是访问和解析 JSON 资源的 AFNetworking 等价物(在您的情况下,将 AFXMLRequestOperation 替换为 AFJSONRequestOperation)

NSURL *url = [NSURL URLWithString:@"https://alpha-api.app.net/stream/0/posts/stream/global"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) 
    NSLog(@"App.net Global Stream: %@", JSON);
 failure:nil];
[operation start];

【讨论】:

以上是关于ios Web 服务登录返回空的 xml 响应的主要内容,如果未能解决你的问题,请参考以下文章

如何将 MaxDate 和 MinDate 设置为从 ios 上的 web 服务返回的 DatePicker

从PHP登录请求获取空的AJAX响应

从 Web 服务响应中解析单个 XML 节点

使用 REST XML Web 服务

使用带网关保护的 JSON Web 服务时 IOS 无响应

返回 JSON 响应的 SOAP Web 服务的 URL 格式