NSMutableUrlRequest 및 특수 문자가있는 암호는 작동하지 않습니다
-
06-07-2019 - |
문제
웹 서비스와 통신하는 작은 프로그램 (코코아 터치 사용)을 작성하고 있습니다. 웹 서비스를 호출하기위한 코드는 다음과 같습니다.
- (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"예제와 같은 특수 문자로 구성되면 웹 서비스가 응답하지 않습니다. "test2009"와 같은 것이면 잘 작동합니다. 누구든지 왜 그런지에 대한 해결책이 있습니까?
해결책
특수 문자는 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
다른 팁
URL을 만들 때 문자열에서 URL 안전 문자를 사용해야합니다.이 nsstring 방법을 사용하십시오. StringByAddingPercateSusingEncoding : 당신을 위해 이것을 할 것입니다.
제휴하지 않습니다 StackOverflow