开发者

How to post a string to web server url in iphone sdk?

开发者 https://www.devze.com 2023-01-30 21:14 出处:网络
How can I开发者_Python百科 post a string(i.e) a word to web server url in iphone sdk? Some sample codes or tutorials would be appreciated.

How can I开发者_Python百科 post a string(i.e) a word to web server url in iphone sdk?

Some sample codes or tutorials would be appreciated.

Thanking you.


This may help you, although I havent tested it:

NSMutableString *httpBodyString;
NSURL *url;
NSMutableString *urlString;

httpBodyString=[[NSMutableString alloc] initWithString:@"Name=The Big Bopper&Subject=Hello Baby&MsgBody=...You knooow what I like...Chantilly lace..."];
urlString=[[NSMutableString alloc] initWithString:@"http://www.somedomain.com/contactform.php"];

url=[[NSURL alloc] initWithString:urlString];
[urlString release];

NSMutableURLRequest *urlRequest=[NSMutableURLRequest requestWithURL:url];
[url release];

[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[httpBodyString dataUsingEncoding:NSISOLatin1StringEncoding]];
[httpBodyString release];

NSURLConnection *connectionResponse = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

if (!connectionResponse)
{
    NSLog(@"Failed to submit request");
}
else
{
    NSLog(@"--------- Request submitted ---------");
    NSLog(@"connection: %@ method: %@, encoded body: %@, body: %a", connectionResponse, [urlRequest HTTPMethod], [urlRequest HTTPBody], httpBodyString);
    NSLog(@"New connection retain count: %d", [connectionResponse retainCount]);
    responseData=[[NSMutableData data] retain];
    NSLog(@"response", responseData);
}

Source: http://www.iphonedevsdk.com/forum/iphone-sdk-development/6341-help-executing-http-url-post-variables.html


You can post the string to webservice in so many ways, In those one is Using Rest protocol:

It has two sub types, HTTP GET, HTTP POST. The Http Get is simple.

You can add the value to the attribute and you can directly call the service. Check the following code.

NSString *url = @"http://123.456.789.0?action=get_movies";

Here the I am passing the get_movies string to the server to the action attribute. and then follow the code for requesting to server.

NSURL *reqUrl = [[NSURL alloc] initWithString:url];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:reqUrl];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSStringEncoding responseEncoding = NSUTF8StringEncoding;
if ([response textEncodingName]) {
CFStringEncoding cfStringEncoding = CFStringConvertIANACharSetNameToEncoding((CFStringRef)[response textEncodingName]);
if (cfStringEncoding != kCFStringEncodingInvalidId) {
    responseEncoding = CFStringConvertEncodingToNSStringEncoding(cfStringEncoding); 
    }
}
[reqUrl release];
NSString *dataString = [[NSString alloc] initWithData:data encoding:responseEncoding];

the dataString is the responseString from the server. You can use that.

Regards,

Satya.

0

精彩评论

暂无评论...
验证码 换一张
取 消