0

I'm an ios noob.. i'm developing an iphone app that uses a soap web service to do the login operation. I read lots of similar questions but none helped me.. I copy-pasted the soap official code example, changing only the url and the request's xml in order to take username and password from two textviews. Connection is ok and i have http error 200 (not a real error..i know, so the connection is ok) but returned webData object length is 0 bytes and so i have an empty xml response..

Here is my code:

- (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];
}

thanks for the help

4

1 回答 1

0

Sorry for not directly answering your question, but I would highly recommend against writing custom networking/parsing code when there are a number of well developed and highly tested libraries that can handle the dirty work for you. Check out AFNetworking, it will handle the dirty work of the connections and XML parsing for you, while using the convenience of blocks.

For example, here's the AFNetworking equivalent of accessing and parsing a JSON resource (substitute AFXMLRequestOperation for AFJSONRequestOperation in your case)

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];
于 2013-06-13T19:54:06.240 回答