0

Webサービスと通信する小さなプログラム(Cocoa Touchを使用)を作成しています。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];
    }
}

ユーザー名とパスワードは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];

これはURLを要求しますhttps://tester:%25=&=-Test2009@example.com

于 2009-09-12T19:21:18.680 に答える