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