【问题标题】:NSMutableURLRequest and a Password with special Characters won't workNSMutableURLRequest 和带有特殊字符的密码不起作用
【发布时间】:2010-11-27 19:13:53
【问题描述】:

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

- (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”,它可以正常工作。有谁知道为什么,也许有解决方案?

【问题讨论】:

    标签: iphone cocoa-touch web-services rpc


    【解决方案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];
    

    这请求 URL https://tester:%25=&amp;=-Test2009@example.com

    【讨论】:

    • 尽管你应该完全没有必要做这一切。相反,使用 -connection:didReceiveAuthenticationChallenge: 委托方法发送用户名和密码;不要把它混入 URL。
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-05
      • 2021-10-12
      • 2014-03-03
      • 2016-11-12
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      相关资源
      最近更新 更多