大家好,我刚接触iphone不久,现在有一个http通讯的任务。
想问各位高手,实现http需要什么类?
各位能否贴出示例源码?
谢谢。。
想问各位高手,实现http需要什么类?
各位能否贴出示例源码?
谢谢。。
解决方案 »
- 做IOS软件开发,对电脑有什么要求啊?
- 基础问题
- 故事板有缓存?
- Cocos2d-x游戏引擎实战开发炸弹超人项目教程分享
- ios 程序内部 自动切换 到指定WIFI 接入点联网 能否实现
- NSDictionary转NSMutableDictionary的问题
- ios微信支付开发遇到的问题
- Xcode addChildViewController 与 addSubview
- 关于windows下用CodeBlock搭建Objective-C开发环境的问题
- UITableViewCell选中后其下的UILabel标签背景色消失
- 求问,windows下有没有用Objective-C写的应用.
- iPhone里pdf的文字搜索和批注能否实现?
NSString *urlString = [NSString stringWithFormat:@"http://urlToSend.com"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
//set headers
NSString *contentType = [NSString stringWithFormat:@"text/xml"];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"]; //create the body
NSMutableData *postBody = [NSMutableData data];
[postBody appendData:[[NSString stringWithFormat:@"<xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"<yourcode/>"] dataUsingEncoding:NSUTF8StringEncoding]];
[postBody appendData:[[NSString stringWithFormat:@"</xml>"] dataUsingEncoding:NSUTF8StringEncoding]];
//post
[request setHTTPBody:postBody];
//get response
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
NSLog(@"Response: %@", result);
//here you get the response }