Sending a request from iphone to webserver - iphone

This may be a simple question but i'm a newer to web service programming.
What i need is to send a recipe to a database and the administrator has to approved it.
I'm handling with the server side program for the first time.sorry for this.
So can anyone kindly help me how to send large datas from iphone to webservice.
Thanks in advance.

For example : Suppose You want to pass user name and password to server. Then you can pass it with the web services. By :
NSString *urlStr = [NSString stringWithFormat:#"%#?method=login&emailId=%#&Password=%#&iphonekey=%#",aWebserviceURL,username,password,aIphoneKey];
NSLog(#"Login - %#",urlStr);
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
NSMutableData *responseData;
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *responseString = [[[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding] autorelease];
NSLog(#"%#",responseString);
Here username,password,aIphoneKey are the Parameters of web service.

I'm assuming you're meaning from an iPhone app to webserver?
If that's the case there's a decent article on making web requests in Objective C here
And a good intro here also

Related

Getting Metadata from shoutcast iOS programming

Hello I am trying to parse Shoutcast radio's metadata in iOS.
After trying many submitted solutions, I end up with a piece of code that is still giving me error
Response String: ICY 404 Resource Not Found
icy-notice1:SHOUTcast Distributed Network Audio Server/Linux v1.9.8
icy-notice2:The resource requested was not found
the code im trying to parse metadata
NSURL *url = [NSURL URLWithString:#"http://relay.181.fm:8052/7.html"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15.0];
[request addValue:#"1" forHTTPHeaderField:#"icy-metadata"];
[request addValue:#"Winamp 5/3" forHTTPHeaderField:#"User-Agent"];
[request addValue:#"audio/mpeg" forHTTPHeaderField:#"Content-Type"];
[request setHTTPMethod:#"GET"];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString* responseString = [[NSString alloc] initWithData:data encoding: NSUTF8StringEncoding];
NSLog(#"Response String: %#", responseString);
any ideas about problem, thanks for helping
Not all SHOUTcast servers allow access to 7.html. There are two other ways to get that data.
The XML data for SHOUTcast is generally available, but requires the server's password. You can request it using /admin.cgi?mode=viewxml&page=4.
You can also read the metadata right out of the stream. This is more cumbersome, but entirely possible, and not too difficult. See this answer for details: https://stackoverflow.com/a/4914538/362536
I found a solution for those who can't/doesn't want to read metadata from stream.
Its the easiest solution I have seen.
http://www.fvalente.org/blog/2012/03/15/shoutcast-metadata-the-easy-way/
Brad says in the post above
Not all SHOUTcast servers allow access to 7.html.
so it is better to check if the server you want to get metadata has /7.html page
the current song is also displayed on the page /played.html but it works in a web browser along with /7.html. But when i tried in fiddler2 on a windows machine i got the ICY 404 resource not found error

Post comment to WordPress Blog from iPhone programmatically

I installed the WordPress blog on my localhost server and also made an iPhone app to browse the blog via rss. I tried to post a comment programmatically using this code.
#define post_url #"http://localhost/web-wp/wp-comments-post.php"
#define post_content #"comment_post_ID=%#&comment_parent=%#&author=%#&email=%#&comment=%#"
NSString *post_str = [NSString stringWithFormat:post_content, #"1", #"0", #"Viet", #"vietnt88#gmail.com", #"test. comment written on mobile"];
NSData *data = [post_str dataUsingEncoding:NSUTF8StringEncoding];
NSURL * url = [NSURL URLWithString:post_url];
NSMutableURLRequest *req = [[NSMutableURLRequest alloc] initWithURL:url];
[req setHTTPMethod:#"POST"];
[req setHTTPBody:data];
NSURLResponse *response;
NSError *err;
[NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&err];
I need this code to work when the user is not logged in. How do I achieve that?
How can I post comment from iPhone?
First of all, if you use "localhost" from your code running on your iPhone, then "localhost" will refer to the iPhone not to your web server. Put there the IP of your server, if you have a public IP than that one otherwise connect your iPhone via WiFi to the same LAN as your local server and use the IP of that server (I guess it'll be something like 192.168...).

How to store the values in web server from i phone application

I am developing view based application.In my view i have register page in that page i have
some fields like Firstname, lastname,e mail ID. When we click save button after entering the values these all field should be store in in webserver.In webserver i have application that application was developed using .net MVC Architecture and database is MYSQL .How i can store these values in webserver.
thanks for your response i have written this code is it right way to store values in webserver
-(IBAction)buttonClick:(id)sender
{
NSString* firstname = nameInput.text;
NSString* lastname = passInput.text;
NSString* bname = lastInput.text;
NSString *post =
[[NSString alloc] initWithFormat:#"fname=%#&lname=%#&email=%#",firstname,lastname,bname];
NSData * postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:NO];
NSString * postLength = [NSString stringWithFormat:#"%d",[postData length]];
NSMutableURLRequest * request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.yoursite.com/file.php?%#",post]]];
[request setHTTPMethod:#"POST"];
[request setValue:postLength forHTTPHeaderField:#"Content-Length"];
[request setValue:#"application/x-www-form-urlencoded" forHTTPHeaderField:#"Content-Type"];
[request setHTTPBody:postData];
NSURLConnection * conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) NSLog(#"Connection Successful");
}
#end
You have two options:
1) Develop a proper API that your iOS app can call using libraries like ASIHTTPRequest or AFNetworking
2) Have a .NET form processor in place, just like you would to process an HTML form, and then use ASIHTTPRequest, AFNetworking, or something similar to submit the request to this processor using the POST method and with the parameters you would like to store added to the request. This simulates an HTML form that has been filled and submitted, and then you can do whatever you wish with the data .NET receives. Both ASI and AFNetworking have pretty extensive documentation on how these types of requests are implemented on iOS with their respective libraries. Unfortunately, ASI is no longer being maintained, so I would recommend going with AFNetworking if possible.
RESPONSE TO UPDATE:
I only gave it a quick look over, but everything looks good to me. The only thing I would change is this:
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.yoursite.com/file.php?%#",post]]];
to this:
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.yoursite.com/file.php"]]];
What you will need now is code on the web side in that file.php that can process the request. It would use the typical $_POST['var_name_here'] to grab the data that is passed to it.
You have to implement a webapplication in your preferred language and deploy it to some webserver or hosting service. To get started, you should choose a language PHP, Java, Python, C# or whatever and google how you could implement a webservice within that language.
I would do this in Java and implement a RESTful wbeservice that sends and receives JSON or XML.

Sending post request does not send post body to server

I have the following code used to post data to a remote server:
NSString *urlStr = [[NSString alloc] initWithFormat: #"http://www.myserver.com/%#",program ];
NSString *postBody =[[NSString alloc] initWithFormat:#"uid=%#",parms];
NSMutableURLRequest *request;
NSData *postData = [postBody dataUsingEncoding:NSISOLatin1StringEncoding];
NSError *error;
NSURLResponse *response;
NSData *dataReply;
request = [NSMutableURLRequest requestWithURL: [NSURL URLWithString:urlStr]];
[request setHTTPMethod: #"POST"];
[request setHTTPBody:postData];
dataReply = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
serverResponse = (NSString *)[[NSString alloc] initWithData:dataReply encoding:NSUTF8StringEncoding];
[postBody release];
[urlStr release];
program is the php program to run on the server. parms is the string to be posted in the format:
\ntoken=12345
Here's the problem: This works fine from the simulator. It works fine on my 3gs development phone. It works fine on a beta tester's 3gs phone that I emailed the app to. It does NOT work fine on another beta tester's iPhone 4 (emailed to him the exact same provision file and ipa file). The POST call is made from the iPhone 4 to the server but no post body is passed. I log each of the calls to the server on the server. The post body is there for the 3gs phones, not for the 4 phone. There are no errors generated in the Apache logs.
Is there some difference between the two phone models that I need to account for? I have tried both http and https access with the same behavior. I don't currently have access to an iPhone 4 so any insight would be a great help.
The answer is not model related. It has to do with the name of the phone which is passed as a parameter to the server. The name contains an apostrophe as in Mike's iPhone.

How to Fetch Data From a WebService in iPhone?

I have to develop an application which includes following things,
=> Make a request to the Web Service through an iPhone...
=> fetch Data from web service...
I have never used an web service to develop iPhone application.
But i know what is web service.
The example of web service is given below. a snapshot
To retrieve data from the webservice you can use NSURLRequest or NSMutableURLRequest
Here is a reference
Along with NSURLConnection
...where you can use methods such as + sendSynchronousRequest:returningResponse:error: or sendAsynchronousRequest. If you are simply doing a get, you can retrieve your xml or json in a very easy way using [NSString s tringWithContentOfURL:url] this will read in the response into the string you assign it to.
I developed some REST services using ASP.NET MVC to return XML documents that were created using Apple's native plist schema. The iphone can very naturally parse plists into their native types. plist is a little verbose compared to JSON, but I don't think it's that much more payload overhead.
If you already have some SOAP web services, then you will have to build your own custom, domain-specific XML parser.
-MrB
U can do it this way...
-(NSString* )createWebServiveRequest:(NSString *)urlLink{
NSURL *url = [NSURL URLWithString:urlLink];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReturnCacheDataElseLoad timeoutInterval:60];
NSData *returnData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse: nil error: nil ];
NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
return responseString; }
call the above method with the nsstring containing url.....and capture it in the method call.