bad URL Connection Error - iphone

I am at my wits end with this one. I am attempting to make a connection to a website database. The URL I am using is: "https://subdomain.domain.com/scripts/script.php?v=YES&userid=xxxxxxxxxx&aid=yyyy&msg=test message"
Prior to sending the URL I encode the string as follows:
NSString *strURL = [url stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
url = [url stringByReplacingOccurrencesOfString:#" " withString:#"%20"];
theURL = [NSURL URLWithString:strURL];
When I attempt to connect to the server I get the following error message:
NSLocalizedDescription = "bad URL";
NSUnderlyingError = "Error Domain=kCFErrorDomainCFNetwork Code=-1000 \"bad URL\" UserInfo=0xa11d760 {NSLocalizedDescription=bad URL
I cannot for the life of me figure out what is going on. Here are some of the actions I have taken to troubleshoot:
I have tested other URLs in the app encoded the same way and have no problem.
I entered the URL directly in Safari, Google and Mozilla with success.
When I tested the URL with curl in the Terminal I discovered the key-value pairs in the query where not being assigned, for example userid was null.
When I pasted escaped text into the curl command line the result was successful.
I do not know what to do here. Any thoughts are greatly appreciated!

I figured out the problem -- seems the URL set to the connection was not escaped properly. I passed the wrong string value to the method. As you can see in the code I passed URL instead of strURL.
Thanks for your help!

Related

how to place arabic in NSURLConnection

I have to put arabic in NSURLConnection, but the problem is that when I am not able to see arabic string properly using NSLog I get following string.
Country is الإمارات العربية المتحدة
and if I pass from NSURLConnection I get
http://xxxxxx.com/api/api.php?func=showAllCompaniesCategoriesCountryWise&params[]=الإمارات العربية المتحدة&params[]=all
bad URL
Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x2f0b00 {NSUnderlyingError=0x2f1130 "bad URL", NSLocalizedDescription=bad URL}
So, please help me, how can I solve this problem.
You should use - (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding to create a proper string for NSURL.

Error while reading JSON url in nsstring

i am sending one url to the server
http://ogcitsco.w05.winhost.com/Service.svc/getsubmenu/south indian/restaurant/Lunch
in which i am getting bad url response, since there is one space between two words south indian, how should i work with that, i have tried to remove the space and passed the url however in response getting null value, when i paste the above url into browser it is showing the response
Remember to encode your URL first: [#"http://ogcitsco.w05.winhost.com/Service.svc/getsubmenu/south indian/restaurant/Lunch" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
a space can be changed to %20 for most languages (or + is possible too i beleve)

Bad URL when requesting with NSURL

I'm trying to request this kind of URL in iPhone 4.0 SDK (access token slightly modified because you don't really need to see it):
https://graph.facebook.com/me?sdk=ios&sdk_version=2&access_token=123902817987|8cb9e8408d2685cef853cd80.9-747882379|UGu5NvcAHiXuGEGzkq&format=json&limit=40&until=1286619821
But I got this message:
Failed with error: Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0x9e657a0 {NSUnderlyingError=0x9e656a0 "bad URL", NSLocalizedDescription=bad URL}
When I copied and pasted in Safari or Chrome, it works. I tried replacing | with %| as suggested here but doesn't work.
Asking it from the terminal looks like this:
curl https://graph.facebook.com/me/statuses?sdk=ios&sdk_version=2&access_token=9999955817987|8ab9e8408d2685cef.3-747882379|UGuxWDuM&format=json&limit=40&until=1286619821
[1] 16190
[2] 16191
[3] 16194
[4] 16195
[5] 16196
[2] Done sdk_version=2
[4]- Done format=json
$ -bash: UGu5NvcAHiXuGEGzkq3KP8xWDuM: command not found
-bash: 8ab9e8408d2685cef853cd80.3-747882379: command not found
{"error":{"type":"OAuthException","message":"An active access token must be used to query information about the current user."}}
[1] Done curl https://graph.facebook.com/me/statuses?sdk=ios
[3]- Exit 127 access_token=9999955817987 | 8ab9e8408d2685cef.3-747882379 | UGuxWDuM
[5]+ Done limit=40
Any ideas?
NSString *strURL = [loc_address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
// requesting weather for this location ...
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat: #"http://www.google.com/ig/api?weather=%#", strURL]] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
[req setHTTPMethod:#"POST"];
[req addValue:#"text/xml; charset=utf-8" forHTTPHeaderField:#"Content-Type"];
You need to replace the pipe character in the URL with %7C. This is the URL encoded value.
Also, the URL needs to be enclosed in quotation marks in order for it to work with curl.
ho ho ho.
Set location_id using the id of the venue you want.
This make me waste a lot of time.
One of dozen facebook secrets is debug network when making what you want, in this case creating a event. You'll see the correct params names for what you need.
This error message, also can be caused by any error in the URL typing. Usually some white space in the URL string.

iOS Develoment: Why is my NSURLConnection failing with a "bad URL" error for only some users?

I have an iOS app that requests JSON data from my Rails 3 app, hosted on Heroku, and it works great on my device and for many other users, except one. I have one user who has told me that my app fails to retrieve the JSON data, so I had her send me some log data and the log showed the NSURLConnection delegate method didFailWithError is being called and the error description reads "bad URL". Why is this error occurring and why is it ONLY occurring on just some devices instead of all devices?
Here's my code,
-(void)getTournamentInfoWithUsername:(NSString*)username
{
NSString *urlString = [NSString stringWithFormat:#"http://myapp-tourney.heroku.com/tournaments/next.json?username=%#", username];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:30];
[self setUrlConnection:[[NSURLConnection alloc] initWithRequest:request delegate:self]];
}
- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
{
[MyLog logError:[NSString stringWithFormat:#"%# - %# - %# - %#", [error localizedDescription], [error localizedFailureReason], [error localizedRecoveryOptions], [error localizedRecoverySuggestion]]];
}
and the log shows...
bad URL - (null) - (null) - (null)
Thanks so much for all your wisdom!
It doesn't look like you are encoding the username. Are there spaces or other special characters in the username? Look into using the NSString method stringByAddingPercentEscapesUsingEncoding:.
It is likely that this depends on the specific URL containing characters that are not allowed for a URL.
You can try and escape the URL:
- (NSString *)stringByAddingPercentEscapesUsingEncoding:(NSStringEncoding)encoding`
I'm not sure how if at all it relates to your issue, but changing the Region in the Settings App had an affect on my app's ability to download. It was spitting out the same Bad URL error if the region was not set to United States. It might explain why some users are getting errors and not others.
This kind of issue may will come due to argument data having special characters(ie. Andy & Co). SOAP automatically will reject this request and returns Bad URL or Bad Request. So, before you sending the data please ensure the data having special characters like &,<,>...
If you found & in your string data , replace with <&>amp; to that string(eg.Andy <&>amp; Co). Hope this input will help for you.
Note: Before you use,Please remove start(<>) and end tag with that symbol.

objective c http query

Hey, Iam new to objective c and really dont know much about it. I have been given a query that is gonna I need to send data in to the server.Query is like this http://abc.com/insertipademail.php?name=name&email=email I need to enter the name and email I have constructed the string But I dont know how to send it to the server. Can someone help me out please. Or point me in the right direction. Thanks
For starters, take a look a NSString's stringWithContentsOfURL:encoding:error: method. You could do something like:
// NSString * myURLString = whatever you do to build the url
NSURL * myURL = [NSURL URLWithString: myURLString];
NSString * response = [NSString stringWithContentsOfURL: myURL encoding: NSUTF8StringEncoding error: NULL];
NSLog(#"The response was: %#", response);
As written, this will ignore all errors and perform the request synchronously. In a real app, you probably want to handle any error that occur, and perhaps perform the request in the background. See the URL Loading System Programming Guide for further documentation. You can also try using any of several open source libraries such as those suggested in David M.'s answer.
I like the library ASIHTTPRequest. There is also HTTPRiot.