首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用目标-C创建POST/GET请求

使用目标-C创建POST/GET请求
EN

Stack Overflow用户
提问于 2011-09-05 22:06:49
回答 2查看 5.2K关注 0票数 0

可能重复:

Tutorials for using HTTP POST and GET on the iPhone in Objective-C

是否可以创建一个具有正确信息的NSArray,如id = 1,name =@John,score = 100,然后发送它并从服务器接收响应?

可能在NSLog()中显示它;

有人能帮我回答这个问题吗?我也不想使用ASIHTTPRequest。我知道这要简单得多,但是如果不使用大量预先编写的代码id就可以完成一些事情,那么最好在使用别人的类之前,先学习如何使用基础框架提供的功能来做一些事情。

EN

回答 2

Stack Overflow用户

发布于 2011-09-05 22:14:13

您要寻找的是NSMutableURLRequest和addValue:forHTTPHeaderField方法。

使用您希望与之通信的URL创建请求。将希望传输的值加载到标头或HTTPBody中,设置HTTPMethod,然后使用NSURLConnection方法发送和接收响应。

对于包含信息的数组,您可以简单地通过数组枚举并将值添加到HTTPHeaderFields中。这实际上取决于服务器要接收的是什么。

http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/URLLoadingSystem.html#//apple_ref/doc/uid/10000165i

有更多的信息。

代码语言:javascript
复制
NSString *urlString = @"http://yoururl.com";
NSURL *url = [NSUL URLWithString:urlString];
NSMutalbeURLRequest *request = [NSMutableURLRequest requestWithURL:url];

NSDictionary *headerInformation = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"id",@"John",@"name",@"100",@"score", nil];
for (NSString *key in [headerInformation allKeys])
    {
        [request addValue:[dict valueForKey:key] forHTTPHeaderField:key];
    }
    NSHTTPURLResponse *response = nil;
    NSError *error = nil;
    // this will perform a synchronous GET operation passing the values you specified in the header (typically you want asynchrounous, but for simplicity of answering the question it works)
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request reuturningResponse:&response error:&error];
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    NSLog(@"Response: %@", responseString);
    [responseString release];
票数 2
EN

Stack Overflow用户

发布于 2011-09-05 22:22:10

使用NSData发送url请求并存储响应可能更容易,然后再重新创建轮子。下面是一些类似于我的生产项目的代码:

代码语言:javascript
复制
+ (NSData *)getProfiles {
    NSString *token = [[NSUserDefaults standardUserDefaults] objectForKey:@"token"];

    //  Create string of the URL
    NSString *serviceURL = [NSString stringWithFormat:@"http://www.myurlhere.com/getProfiles.php?token=%@", token];
    NSLog(@"Service URL : %@", serviceURL);

    //  Create a NSURL out of the string created earlier.  Use NSASCIIStringEncoding to make it properly URL encoded (replaces " " with "+", etc)
    NSURL *URL = [NSURL URLWithString:[serviceURL stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];

    //  Request the url and store the response into NSData
    NSData *data = [NSData dataWithContentsOfURL:URL];
    if (!data) {
        return nil;
    }

    //  Since I know the response will be 100% strings, convert the NSData to NSString
    NSString *response = [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] autorelease];

    //  Test response and return a string that an XML Parser can parse
    if (![response isEqualToString:@"UNAUTHORIZED"]) {
        response = [response stringByReplacingOccurrencesOfString:@"&" withString:@"&"];
        data = [response dataUsingEncoding:NSASCIIStringEncoding];
        return data;
    } else {
        return nil;
    }
}

NSLog输出:

代码语言:javascript
复制
[Line: 476] +[WebSupport getProfiles]: Service URL : http://www.myurlhere.com/getProfiles.php?token=abcdef0123456789abcdef0123456789
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7313123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档