Domanda

Sto scrivendo un piccolo programma (usando Cocoa Touch), che comunica con un servizio web. Il codice per chiamare il servizio web è il seguente:

- (IBAction)send:(id)sender
{
    if ([number.text length] > 0)
    {
        [[UIApplication sharedApplication] beginIgnoringInteractionEvents];  
        [activityIndicator startAnimating];
        NSString *modded;
        modded = [self computeNumber];
        NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:      [NSURL URLWithString:@"https://tester:%=&=-Test2009@samurai.sipgate.net/RPC2"]];
        [theRequest setHTTPMethod:@"POST"];
        [theRequest addValue:@"text/xml" forHTTPHeaderField:@"content-type"];
        [theRequest setCachePolicy:NSURLCacheStorageNotAllowed];
        [theRequest setTimeoutInterval:5.0];
        NSString* pStr = [[NSString alloc] initWithFormat:@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><methodCall><methodName>samurai.SessionInitiate</methodName><params><param><value><struct><member><name>LocalUri</name><value><string></string></value></member><member><name>RemoteUri</name><value><string>sip:%@@sipgate.net</string></value></member><member><name>TOS</name><value><string>text</string></value></member><member><name>Content</name><value><string>%@</string></value></member><member><name>Schedule</name><value><string></string></value></member></struct></value></param></params></methodCall>", modded, TextView.text];
        NSData* pBody = [pStr dataUsingEncoding:NSUTF8StringEncoding];
        [theRequest setHTTPBody:pBody];
        NSURLConnection *theConnection = [[NSURLConnection alloc]     initWithRequest:theRequest delegate:self];

        if (!theConnection)
        {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" 
                                                            message:@"A Connection could not be established!"
                                                           delegate:nil 
                                                  cancelButtonTitle:@"OK" 
                                                  otherButtonTitles: nil];
            [alert show];
            [alert release];
            sendButton.enabled = TRUE;
            return;
        }
        [pStr release];
        [pBody release];
    }
}

Il nome utente e la password devono trovarsi nell'URL e nella maggior parte dei casi funziona, ma quando la password è composta da caratteri speciali come nell'esempio "% = & amp; = - Test2009 " ;, il Webservice non risponde. Se è simile a " Test2009 " funziona benissimo. Qualcuno ha un'idea del perché e forse una soluzione per questo?

È stato utile?

Soluzione

I caratteri speciali devono essere codificati nell'URL, l'URL richiesto,

https://tester:%=&=-Test2009@samurai.sipgate.net/RPC2

..non è valido, in particolare la parte % = (% viene utilizzata per i caratteri di escape, ad esempio % 20 viene utilizzato per rappresentare uno spazio), quindi ..

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:
    [NSURL URLWithString:@"https://tester:%=&=-Test2009@samurai.sipgate.net/RPC2"]];

.. dovrebbe cambiare in qualcosa del tipo:

NSString *theAddress = [NSString stringWithFormat:@"https://%@:%@@%@",
                        [@"tester" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                        [@"%=&=-Test2009" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                        @"example.com"];

NSURL *theURL = [NSURL URLWithString:theAddress];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:theURL];

Questo richiede l'URL https://tester:%25=&=-Test2009@example.com

Altri suggerimenti

È necessario utilizzare caratteri URL sicuri nella stringa durante la creazione dell'URL, utilizzare questo metodo NSString stringByAddingPercentEscapesUsingEncoding: che farà ciò per te

.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top