【问题标题】:How to post a string to web server url in iphone sdk?如何在 iphone sdk 中将字符串发布到 Web 服务器 url?
【发布时间】:2010-12-13 13:06:34
【问题描述】:

如何在 iphone sdk 中向网络服务器 url 发布一个字符串(即一个词)?

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

谢谢你。

【问题讨论】:

    标签: iphone


    【解决方案1】:

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

    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);
    }
    

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

    【讨论】:

    • 如果“POST”不起作用(即不是实践方法),也可以尝试上面的“PUT”。
    【解决方案2】:

    您可以通过多种方式将字符串发布到网络服务,其中一种是 使用 Rest 协议:

    它有两种子类型,HTTP GET,HTTP POST。 Http Get 很简单。

    您可以将值添加到属性中,您可以直接调用服务。 检查以下代码。

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

    在这里,我将 get_movies 字符串传递给服务器到 action 属性。 然后按照代码向服务器请求。

    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];
    

    dataString 是来自服务器的响应字符串。你可以使用它。

    问候,

    萨提亚。

    【讨论】:

    • 如何查看设备是否有网络连接?
    猜你喜欢
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 2017-09-26
    • 2015-07-28
    • 2014-07-22
    • 2013-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多