how to send data to server using get request - iphone

I want to send data to server when user fill ups the from in iphone , i used
NSURLRequest
but it returns with bad url when spaces are coming in the from data
any help please

if what you want is just send a form over GET, a possible way is:
NSString* urlWithParams = #"http://xxxxxxxxxx.com/xxxxx/xxxxxx/mobile.php?method=createOrder&sid=dfdk6figau1reagpdv9sc6po67&dnumber=ali.jamali&odate=2011-05-21&ordate=2011-05-21&oshipto=shipopingto&otype=emergency&oatype=&oref=refrence&oshipvia=Shipping";
NSString* escapedUrl = [urlWithParams stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
now use escapedUrl as the URL for your NSURLRequest.
If you need help with the request just tell me. Based on your comments, i see your problem was just the encoding.

Okay, a couple of things. First one: why are you using a GET? A POST is sort of canonical for sending data, like from a form.
Second: are you converting the URL to net form? That is, the non-address characters to their %-sign forms, like %20 for a space?

Related

Sending data to a website and getting results of search iOS

I am very new to iOS and I've just begun reading about HTTP requests and POST and GET methods. Let's say, for example, I want to have the user input a string, and then send that data to a website, (for this example, say www.rhymezone.com), search with that string, and get the results of that search within my application. Is this done with an HTTP post method? Or what? Any help / examples would be greatly appreciated. Also, if there are tutorials for this stuff, that would be appreciated as well.
For sake of example, here is what I've tried:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.rhymezone.com/r/rhyme.cgi?Word=test&typeofrhyme=perfect&org1=syl&org2=l&org3=y"]];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSString *dataAsString=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"data: %#",dataAsString);
}
This outputs the entire source of the website (searching for rhymes of the word test). While I can certainly write a method to go through the source of the website and extract the words it returns, I feel like this is not correct. My way of getting rhymes of different words is simply to change the URL here, so where it says 'test' I change it to whatever the user inputs.
Thanks
Look into AFNetworking and RestKit.
It's easiest if you're calling a public API that uses JSON/XML, and then use a built in parser or a parser library to extract the data you want.
Simply downloading the contents of a URL is an HTTP GET request, such as going to a website.
This link talks a bit more about the difference between GET and POST.
When do you use POST and when do you use GET?
If I understand correctly what you are trying to do, I fear that the only option for you is sending the HTTP request (GET or POST according to what the website expects, just like you are doing) and then parse the result to filter all the information that is not relevant.
An alternative approach would be possible if you were using a website offering a REST API, or a JSON API so that you send the query and you get back just the information you need (in a specific format).
So, it depends strongly on the website you are using, but for the generic case, the only option you have is parsing.
(Or, you could display the full content of the page through UIWebView. This would not require explicitly setting up a connection, but I am not sure it is what you are trying to do.)
You are looking for a way to communicate with your website from your iOS application. The common approach is to get the string entered by the user, encode and send it as http request to a sort of script (webservice). This script will do all the stuff you want (search with this string). Then re-send the result to the client (your iOS app) as a http response which will be parsed in your iOS app(with a JSON parser for instance).
There is good resources around that, as an example, you may read this: http://www.raywenderlich.com/2965/how-to-write-an-ios-app-that-uses-a-web-service

Retrieving response from a php url in iphone

I have created an api. Lets say this is my php url
"http://xxxxxxx/game/game.php?validate=yes&email=myEmail#mars.com"
The response of this query is either 1 0r 0. It validates the email if it exists in db or not. I have been searching but failing till now, How am i suppose to send it to server. By NS URL or I have to use NSURLConnection. How in turn I can read the response.
Best Regards
NSString *response = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://xxxxxxx/game/game.php?validate=yes&email=myEmail#mars.com"]];
Bible, Old Testament.
Bible, New Testament.
Edit: so the response is XML? Right. (No, not right, you should really consider using JSON, but anyways...) You can use the NSXMLParserClass to get back the response in this case. Especially have a look at its - initWithContentsOfURL: method.

Sharekit: url is modified while posting to facebook?

I am posting image with image title and in the image title I have added url, I am able to share image and url also but,there is slight modification in the url.The = symbol is converted to %3D ,as shown below the both URLs(dummy url).
posted URL:
http://....=418ioekVlhTIu2sr9qpdAQ==
 
URL on Facebook
http://...=418ioekVlhTIu2sr9qpdAQ%3D%3D
So is there any better way to post url and image in one post only or
help me so that by doing some change in the code I should be able to share correct url in the Image title itself.
This happens because the URL format converts it's reserved special characters to HTML entity codes (percent escaping) as shown in here:
http://www.w3schools.com/tags/ref_urlencode.asp
you have 2 options to pass the URL string correctly:
On the receiver side (after the URL Request is sent by the client), decode the URL string that you received, this will normalize the string back to normal.
Use the POST method of html instead of the GET method to store your parameters. although I'm not sure you have an option for that.
On the iOS obj-c, Conversion between URL Percent escapes is done like so-
[normalText stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[encodedText stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
Just for fun:
You can enter a URL and see it's encoded/decoded value on this website: http://meyerweb.com/eric/tools/dencoder and see how it works in practice.

When doing a reverse geocoding request Google magically recognizes my iPhone language

Im sending a simple url request using the following url:
urlString = [NSString stringWithFormat:
#"http://maps.googleapis.com/maps/api/geocode/xml?latlng=%f,%f&sensor=true", lat, lng];
Let's say my current location is Europe, Romania, Oradea.
If I set my iPhone to German for example, I get the city name in german. Somehow Google magically knows that my iPhone is German and returns geocoding data in german (Grosswardein). The problem is I would really like to have the city name untranslated (Oradea in romanian). How does google know that my iPhone's language is German, and how do I stop this. I'm not using MKReverseGeocoder because I couldn't turn this feature off but now I'm facing the exact same problem here.
Update: There are no headers sent:
[request allHTTPHeaderFields] returns null. It seems Google is clairvoyant.
Update: This: [request setValue: #"*" forHTTPHeaderField: #"Accept-Language"] seems to work.
As far as I understand from here I should use an asterisk. BTW Wireshark is awesome.
You could try to see if your request includes Accept-Language HTTP headers. Dunno how you send it, but it sounds like it does.

Can I get the status text from an NSHTTPURLResponse?

If I use NSURLConnection to get data from a server and it sends back a response that begins with, say:
HTTP/1.1 406 Some string of text here
Is there any way I can retrieve the status text "Some string of text here"? I know how to get the status code, and I know about localizedStringForStatusCode:, but in this case I need access to the specific text sent back.
You can use ASIHTTPRequest, which provides this as -requestStatusMessage. Or you can use Core Foundation's CFHTTP, which provides CFHTTPMessageCopyResponseStatusLine. I don't believe there's a good way to get to the CFHTTPMessage from NSHTTPURLResponse unfortunately. Of course, ASIHTTPRequest is pretty awesome anyway.