0

我正在编写一个与 Web 服务通信的小程序(使用 Cocoa Touch)。调用网络服务的代码如下:

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

用户名和密码必须在 URL 中,并且它在大多数情况下都有效,但是当密码包含特殊字符(例如示例“%=&=-Test2009”)时,Web 服务不会响应。如果它类似于“Test2009”,它可以正常工作。有谁知道为什么,也许有解决方案?

4

2 回答 2

1

创建 URL 时,您必须在字符串中使用 URL 安全字符,使用此 NSString 方法stringByAddingPercentEscapesUsingEncoding:将为您执行此操作。

于 2009-09-12T19:19:22.530 回答
1

特殊字符必须经过 URL 编码,您请求的 URL,

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

..无效,具体是%=部分(%用于转义字符,例如%20用于表示空格),所以..

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

..应该更改为:

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

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

这请求 URLhttps://tester:%25=&=-Test2009@example.com

于 2009-09-12T19:21:18.680 回答