如何在iphone sdk中将字符串发布到Web服务器URL?

Sar*_*nya 1 iphone

如何在iphone sdk中将字符串(即)单词发布到Web服务器URL?

一些示例代码或教程将不胜感激.

感谢您.

Adn*_*nan 5

这可能对你有所帮助,虽然我还没有测试过:

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);
}
Run Code Online (Sandbox Code Playgroud)

资料来源:http://www.iphonedevsdk.com/forum/iphone-sdk-development/6341-help-executing-http-url-post-variables.html