send string from iphone [duplicate]
Possible Duplicate:
sending post data from Iphone
Hello everyone, I'm a new member of the forum and browsing the web I found what was right fo开发者_运维技巧r me, or rather being a novice I did not understand what poker was.
What I'm trying to do is go to my iphone from a string php server, example: site.com/site.php?val1=1&val2=2 and that the server will recognize it and give me back a xml, but one thing at a time how do I send the string to the server by pressing a button IBAction?
This is what I usually do for that:
NSString *reqURL = [NSString stringWithFormat:@"http://site.com/site.php?val1=%@&val2=%@",var1,var2];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:reqURL]];
NSURLResponse *resp = nil;
NSError *err = nil;
NSData *response = [NSURLConnection sendSynchronousRequest: theRequest returningResponse: &resp error: &err];
Readup in NSURLConnection or use ASIHttpRequest (which is easier to use).
get ASIHTTPRequest from http://allseeing-i.com/ASIHTTPRequest/Setup-instructions and
import
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
and do following to get response
NSURL *url= [NSURL URLWithString:@"http://site.com/site.php"];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
request setPostValue:@"1" forKey:@"val1"];
request setPostValue:@"2" forKey:@"val2"];
[request startSynchronous];
NSString *retVal = nil;
NSError *error = [request error];
if (!error) {
retVal = [request responseString];
}
here you got data on retVal
精彩评论