Pregunta

Estoy escribiendo un pequeño programa (usando Cocoa Touch), que se comunica con un servicio web. El código para llamar al servicio web es el siguiente:

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

El nombre de usuario y la contraseña deben estar en la URL, y funciona en la mayoría de los casos, pero cuando la contraseña consta de caracteres especiales como en el ejemplo "% = & amp; = - Test2009 " ;, el servicio web no responde. Si es algo así como "Test2009" funciona bien. ¿Alguien tiene una idea de por qué, y tal vez una solución para eso?

¿Fue útil?

Solución

Los caracteres especiales deben estar codificados en URL, la URL que solicitó,

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

.. no es válido, específicamente la parte % = (% se usa para escapar caracteres, por ejemplo, % 20 se usa para representar un espacio), entonces ...

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

... debería cambiar a algo como:

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

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

Esto solicita la URL https://tester:%25=&=-Test2009@example.com

Otros consejos

Debe usar caracteres seguros de URL en su cadena al hacer la URL, use este método NSString stringByAddingPercentEscapesUsingEncoding: que hará esto por usted.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top