2

How can i send a url request containing a @ character in the username ?
Currently my url request code looks as follows :

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://user@exam.com:pwd@example.com"]];

The above code is fetching data from site exam.com instead of example.com

How can i fetch data from example.com with user@exam.com as username name and pwd as password ?

I tried escape sequences like \ for escaping the @ character but still its not working.

prajul
  • 1,216
  • 2
  • 15
  • 27

2 Answers2

2

always encode your parameter before making a NSURL from them

 NSString * encodedUsername = (NSString *)CFURLCreateStringByAddingPercentEscapes(
    NULL,
    (CFStringRef)unencodedUsername,
    NULL,
    (CFStringRef)@"!*'();:@&=+$,/?%#[]",
    kCFStringEncodingUTF8 );
Saurabh
  • 22,743
  • 12
  • 84
  • 133
0

You can encode a string before using it to generate a url like below :

NSString * encodedString = [q stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding];

NSURL *requestURL = [NSURL URLWithString:encodedString];
Ugur Kumru
  • 986
  • 6
  • 9