Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSMutableURLRequest和带有特殊字符的密码赢得';行不通_Iphone_Cocoa Touch_Web Services_Rpc - Fatal编程技术网

Iphone NSMutableURLRequest和带有特殊字符的密码赢得';行不通

Iphone NSMutableURLRequest和带有特殊字符的密码赢得';行不通,iphone,cocoa-touch,web-services,rpc,Iphone,Cocoa Touch,Web Services,Rpc,我正在编写一个小程序(使用Cocoa Touch),它与Web服务进行通信。 调用Web服务的代码如下所示: - (IBAction)send:(id)sender { if ([number.text length] > 0) { [[UIApplication sharedApplication] beginIgnoringInteractionEvents]; [activityIndicator startAnimating];

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

- (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];
    }
}
-(iAction)发送:(id)发送方
{
如果([number.text length]>0)
{
[[UIApplication sharedApplication]beginIgnoringInteractionEvents];
[活动指示器启动激活];
NSString*已修改;
modded=[自计算编号];
NSMutableURLRequest*theRequest=[NSMutableUrlRequestWithURL:[NSURL URLWithString:@]https://tester:%=&=-Test2009@samurai.sipgate.net/RPC2“]];
[TheRequestSetHttpMethod:@“POST”];
[TheRequestAddValue:@“文本/xml”用于HttpHeaderField:@“内容类型”];
[请求集缓存策略:不允许NSURLCacheStorage];
[TheRequestSetTimeOutInterval:5.0];
NSString*pStr=[[NSString alloc]initWithFormat:@“samurai.SessionInitiateLocalUrireMotourisp:%@@sipgate.netTOStextContent%@Schedule”,moded,TextView.text];
NSData*pBody=[pStr数据使用编码:NSUTF8StringEncoding];
[请求集httpbody:pBody];
NSURLConnection*连接=[[NSURLConnection alloc]initWithRequest:theRequest委托:self];
如果(!连接)
{
UIAlertView*警报=[[UIAlertView alloc]initWithTitle:@“错误”
消息:@“无法建立连接!”
代表:无
取消按钮:@“确定”
其他按钮:无];
[警报显示];
[警报发布];
sendButton.enabled=TRUE;
返回;
}
[pStr发布];
[pBody发布];
}
}

用户名和密码必须在URL中,并且在大多数情况下都有效,但是当密码由特殊字符组成时,如示例“%=&=-Test2009”,Web服务不响应。如果它是类似于“Test2009”的东西,那么它可以正常工作。有人知道原因吗?可能有解决方案吗?

在创建URL时,您必须在字符串中使用URL安全字符,使用此NSString方法将为您做到这一点。

必须对特殊字符进行URL编码,即您请求的URL

https://tester:%=&=-Test2009@samurai.sipgate.net/RPC2
..无效,特别是
%=
部分(
%
用于转义字符,例如
%20
用于表示空格),因此

…应更改为以下内容:

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=&=-Test2009@example.com

尽管你根本不需要做这些。相反,使用-connection:didReceiveAuthenticationChallenge:delegate方法发送用户名和密码;不要把它混入URL。
NSString *theAddress = [NSString stringWithFormat:@"https://%@:%@@%@",
                        [@"tester" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                        [@"%=&=-Test2009" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding],
                        @"example.com"];

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