简单的objective

编程入门 行业动态 更新时间:2024-10-26 08:27:58
本文介绍了简单的objective-c GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

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

NSData * data = [通知对象];

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

$ b $的顺序b

example/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

example/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

本文发布于:2023-10-12 16:30:50,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:简单   objective

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!