在Objective-C中的iPhone上使用HTTP POST和GET的教程 [英] Tutorials for using HTTP POST and GET on the iPhone in Objective-C

查看:124
本文介绍了在Objective-C中的iPhone上使用HTTP POST和GET的教程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下载了使用HTTP POST和GET的苹果演示程序(它们的示例应用程序有一个带有不同部分的tabbar),代码非常混乱!

有人可以给我一些示例代码或有关它的一些教程的链接? :)



谢谢!

解决方案

特别好:
http:/ /www.cimgf.com/2010/02/12/accessing-the-cloud-from-cocoa-touch/

  NSMutableURLRequest * request = [[NSMutableURLRequest alloc] 
initWithURL:[NSURL
URLWithString:@http://www.cimgf.com/testpost.php]];

[request setHTTPMethod:@POST];
[request setValue:@text / xml
forHTTPHeaderField:@Content-type];

NSString * xmlString = @< data>< item> Item 1< / item>< item> Item 2< / item>< / data>;

[请求setValue:[NSString stringWithFormat:@%d,
[xmlString length]]
forHTTPHeaderField:@Content-length];

[request setHTTPBody:[xmlString
dataUsingEncoding:NSUTF8StringEncoding]];

[[NSURLConnection alloc]
initWithRequest:request
delegate:self];


I downloaded apple's demo for using HTTP POST and GET (Their sample app has a tabbar with different parts) and the code is so confusing!

Could anybody give me some sample code or a link to some tutorials about it? :)

Thanks!

解决方案

This walkthrough by Matt Long is particularly good: http://www.cimgf.com/2010/02/12/accessing-the-cloud-from-cocoa-touch/

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
        initWithURL:[NSURL 
        URLWithString:@"http://www.cimgf.com/testpost.php"]];
 
[request setHTTPMethod:@"POST"];
[request setValue:@"text/xml" 
              forHTTPHeaderField:@"Content-type"];
 
NSString *xmlString = @"<data><item>Item 1</item><item>Item 2</item></data>";
 
[request setValue:[NSString stringWithFormat:@"%d",
        [xmlString length]] 
        forHTTPHeaderField:@"Content-length"];
 
[request setHTTPBody:[xmlString 
        dataUsingEncoding:NSUTF8StringEncoding]];
 
[[NSURLConnection alloc] 
        initWithRequest:request 
                   delegate:self];

这篇关于在Objective-C中的iPhone上使用HTTP POST和GET的教程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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