简单的objective-c GET请求 [英] Simple objective-c GET request

查看:522
本文介绍了简单的objective-c GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的大部分信息都是指废弃的ASIHTTPREQUEST项目,所以请原谅我再次提出要求。



实际上,我需要刷磁条并发送轨道2数据到一个返回登记或notenrolled(取决于卡的状态...)的web服务。

所以我的数据简单地表示为

  NSData * data = [通知对象]; 

然后我需要将这个传递给一个url到

$ b $的顺序b

http://example.com/CardSwipe。 cfc?method = isenrolled& track2 = data

然后只收到一个响应字符串...



我搜索了一大堆,似乎有一些矛盾的答案,应该只用AFNetworking,RESTkit还是使用本地NSURL / NSMutableURLRequest协议完成。

解决方案

在Objective-C中执行HTTP请求的选项可能有点吓人。对我来说一个很好的解决方案是使用 NSMutableURLRequest 。一个例子(使用ARC,所以YMMV)是:

$ p $ - (NSString *)getDataFrom:(NSString *)url {
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@GET];
[request setURL:[NSURL URLWithString:url]];

NSError * error = nil;
NSHTTPURLResponse * responseCode = nil;

NSData * oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:& responseCode error:& error]; ([responseCode statusCode]!= 200){
NSLog(@Error%@,HTTP status code%i,url,[responseCode statusCode]);


返回零;
}

return [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding];
}

更新: b

您的问题的标题和标记说POST,但您的示例URL将指示GET请求。在GET请求的情况下,上面的例子就足够了。对于POST,您可以按如下方式进行更改:

   - (NSString *)getDataFrom:(NSString *)url withBody :(NSData *)body {
NSMutableURLRequest * request = [[NSMutableURLRequest alloc] init];
[request setHTTPMethod:@POST];
[request setHTTPBody:body];
[请求setValue:[NSString stringWithFormat:@%d,[body length]] forHTTPHeaderField:@Content-Length];
[request setURL:[NSURL URLWithString:url]];

/ *与上述相同* /
}


Most of the information here refers to the abandoned ASIHTTPREQUEST project so forgive me for asking again.

Effectively, I need to swipe a magnetic strip and send the track 2 data to a webservice that returns "enrolled" or "notenrolled" (depending on the status of the card...)

So my data comes in simply as

NSData *data = [notification object];

And then I need to pass this to a url to the order of

http://example.com/CardSwipe.cfc?method=isenrolled&track2=data

And then just receive a response string...

I've searched a ton and there seems to be some conflicting answers as to whether this should be accomplished simply with AFNetworking, RESTkit, or with the native NSURL/NSMutableURLRequest protocols.

解决方案

The options for performing HTTP requests in Objective-C can be a little intimidating. One solution that has worked well for me is to use NSMutableURLRequest. An example (using ARC, so YMMV) is:

- (NSString *) getDataFrom:(NSString *)url{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setHTTPMethod:@"GET"];
    [request setURL:[NSURL URLWithString:url]];

    NSError *error = nil;
    NSHTTPURLResponse *responseCode = nil;

    NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];

    if([responseCode statusCode] != 200){
        NSLog(@"Error getting %@, HTTP status code %i", url, [responseCode statusCode]);
        return nil;
    }

    return [[NSString alloc] initWithData:oResponseData encoding:NSUTF8StringEncoding]; 
}

Update:

Your question's title, and tagging say POST, but your example URL would indicate a GET request. In the case of a GET request, the above example is sufficient. For a POST, you'd change it up as follows:

- (NSString *) getDataFrom:(NSString *)url withBody:(NSData *)body{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:body];
    [request setValue:[NSString stringWithFormat:@"%d", [body length]] forHTTPHeaderField:@"Content-Length"];
    [request setURL:[NSURL URLWithString:url]];

    /* the same as above from here out */ 
}

这篇关于简单的objective-c GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆