I have a NSURLRequest created next way:
NSString *url=[NSString stringWithFormat:#"http://myserveradress.com/api/users/register?register_password=%#®ister_password_confirm=%#®ister_email=%#®ister_terms=1®ister_user=%#&process_registration=1®ister_name=%#"
,
[password stringByReplacingOccurrencesOfString:#" " withString:#"%"],[confirm stringByReplacingOccurrencesOfString:#" " withString:#"%"],[email stringByReplacingOccurrencesOfString:#" " withString:#"%"],[user stringByReplacingOccurrencesOfString:#" " withString:#"%"],[realName stringByReplacingOccurrencesOfString:#" " withString:#"%"]
];
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSError *error;
NSData *data=[NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];
NSString *result_string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return result_string;
The result string is http://myserveradress.ru/api/users/register?register_password=password®ister_password_confirm=password®ister_email=myname#mail.com®ister_terms=1®ister_user=myname&process_registration=1®ister_name=Artem%Kulikov and the error states
Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x71e62b0 {NSUnderlyingError=0x71e6470 "bad URL", NSLocalizedDescription=bad URL}
In browser the link works fine. The problem is only in the app.
That is not the proper way to escape the strings, there is a method for that, stringByAddingPercentEscapesUsingEncoding:. So change it to:
NSString *url = [NSString stringWithFormat:#"http://myserveradress.com/api/users/register?register_password=%#®ister_password_confirm=%#®ister_email=%#®ister_terms=1®ister_user=%#&process_registration=1®ister_name=%#"
, password, confirm, email, user, realName];
NSString *properlyEscapedURL = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:properlyEscapedURL]];
Not to mention the proper escape code for a space is %20 not %.
#user1506704, you are not use encoding method like in register_name, Are there spaces or other special characters in the register_name? Use NSString method stringByAddingPercentEscapesUsingEncoding:.
You can try and escape the URL:
- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding`
Related
i am trying to pass profile pic url which is in the formate
https://fbcdn-profile-a.akamaihd.net/hpe-ak-xtp1/v/t1.0-1/c1.0.720/p7720/1364291238051_6773172187152486924_n.jpg?oh=6fda2708e5c1a5de47263&oe=585DC8CF&__gda__=1482408d4fc8e65486e8e1c269af042
but when i pass it like
NSString *fbpicurl = #"https://fbcdn-profile-a.akamaihd.net/hpe-ak-xtp1/v/t1.0-1/c1.0.720/p7720/1364291238051_6773172187152486924_n.jpg?oh=6fda2708e5c1a5de47263&oe=585DC8CF&__gda__=1482408d4fc8e65486e8e1c269af042";
NSString *post =[[NSString alloc] initWithFormat:#"profilepic=%#",fbpicurl];
NSURL *url=[NSURL URLWithString:#"http://URL.com"];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
when i pass it this way url do not pass completely it just cut of where if founds & right after oh=6fda2708e5c1a5de47263 please help me how can i pass complete url string.
First encode your url string.
+ (NSString *) addPercentEscapesAndReplaceAmpersand: (NSString *) yourUrlString
{
NSString *encodedString = [yourUrlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
return [encodedString stringByReplacingOccurrencesOfString: #"&" withString: #"%20"];
}
Then used string for url by using NSURL URLWithString
NSString *urlAddress = [NSString stringWithFormat:#"http://www.domain.com?input=%#",[alertView textFieldAtIndex:0].text];
NSURL *myUrl = [NSURL URLWithString:[urlAddress stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *openURL = [NSString stringWithContentsOfURL:myUrl encoding:NSUTF8StringEncoding error:NULL];
NSLog(#"%#",openURL);
openUrl is always returning (null), probably because of the encoding, but I don't know how to fix it.
Double check these three things:
Check that the URL which you are pointing to is a valid URL. The null value could be a result of an invalid URL.
Catch the NSError which the stringWithContentsOfURL throws. That will give you some insight on what is wrong.
Like you suspect, try changing the string encoding.
NSError *error;
NSString *urlString = [NSString stringWithFormat:#"http://www.domain.com?input=%#", stringVariable];
NSURL *urlAdress = [NSURL URLWithString:urlString];
NSString *urlContent = [[NSString alloc] initWithContentsOfURL:urlAdress encoding:NSUTF8StringEncoding error:&error];
NSLog(#"%#", urlContent);
This works fine, the problem was an failing internet connection.
I have a url in an NSString called myUrl. When I NSLog myUrl I see the following:
http://dl.dropbox.com/u/57665723%2FChocolatesRUs%2FJellies%2FUn-sugared%2Fgen%20no%20sugar.pdf
I then try connecting using this url as follows:
NSURL* url = [ NSURL URLWithString:nextFileURL ];
NSMutableURLRequest* request = [ [ NSMutableURLRequest alloc ] initWithURL: url ];
NSURLConnection* conn = [ [ NSURLConnection alloc ] initWithRequest: request delegate: self ];
I am getting the following error:
errorcode=-1000, error=Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x27e540 {NSUnderlyingError=0x26d800 "bad URL", NSLocalizedDescription=bad URL}
I have tried using
NSString* myUrl = [myUrl stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
but this doesn't work either.
Can anyone see the problem?
Maybe you can escape your url with stringByAddingPercentEscapesUsingEncoding instead of stringByReplacingPercentEscapesUsingEncoding.
stringByAddingPercentEscapesUsingEncoding is remove spaces and adding percents
NSString *urlString =[NSString stringWithFormat:#"&street=%#&street2=&city=%#&state=%#&
zipcode=%#&candidates=10", address.address2,address.city, address.state, address.zip5];
NSLog(#"BAD URL - %#",urlString ); // there is space in url
NSString *encodedUrl = [urlString stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
NSLog(#" CORRECT URL - %#", encodedUrl); // url encode that space by %20
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL RLWithString:encodedUrl]];
I'm downloading a webpage using NSMutableURLRequest but having problem putting that very webpage into a NSString.
NSString *username = #"my_username";
NSString *password = #"my_password";
NSURL* url = [NSURL URLWithString:[NSString stringWithFormat:#"https://www.mypage.com/login.php?username=%#&password=%#", username, password]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnPage = [NSString stringWithFormat:#"%.*s", [returnData length], [returnData bytes]];
This works fine except for special chars like åäö etc. Is there any better way?
Yes, use the following:
NSString *returnPage = [[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding] autorelease];
That uses UTF8 instead of ASCII.
I'm attempting to do an http POST that has a key containing a $ char and wanted to know if I'm submitting it correctly
here is the http POST in httpfox (RAW)
&__EVENTTARGET=ctl00%24ContentPlaceHolder1%24btnSearch
here it is the plain txt format (notice the $ chars)
ctl00$ContentPlaceHolder1$btnSearch
but in my string I couldn't get a valid post doing something like this
NSURL *url = [NSURL URLWithString:#"https://www.localhost.com/someurl.aspx"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSString* theBodyString = [NSString stringWithFormat:#"__EVENTTARGET=ctl00$ContentPlaceHolder1$btnSearch"];
NSData *requestData = [theBodyString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPMethod:#"POST"];
//other request details ...
Looks like you need to URL encode theBodyString. Try:
NSString* theBodyString = [NSString stringWithFormat:#"__EVENTTARGET=ctl00$ContentPlaceHolder1$btnSearch"];
NSString* escapedUrlString = [theBodyString stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding];
NSData *requestData = [escapedUrlString dataUsingEncoding:NSUTF8StringEncoding];
or (in response to the comment)
NSString* theBodyString = #"2010 19:32";
NSString* escapedUrlString = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)theBodyString, (CFStringRef)#" ", (CFStringRef)#"!*'();:#&=+$,/?%#[]", kCFStringEncodingUTF8 );
escapedUrlString = [escapedUrlString stringByReplacingOccurrencesOfString:#" " withString:#"+"];