iOS http request sample code
来源:互联网 发布:淘宝开店没客源怎么办 编辑:程序博客网 时间:2024/06/05 20:39
转自:http://hi.baidu.com/leonqgl/blog/item/cee6e3d3c2762b163af3cfeb.html
http://blog.163.com/kongshuai1234@126/blog/static/1103669952010615935072/
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; NSUserDefaults *defs = [NSUserDefaults standardUserDefaults]; NSLog(@"Load currentCookie:%@", cookie); [request setValue:cookie forHTTPHeaderField:@"Cookie"]; [request setURL:[a objectAtIndex:0]]; [request setHTTPMethod:@"GET"]; NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; [request release]; 一、iPhone终端代码:(同步请求)NSString *post = nil; post = [[NSString alloc] initWithFormat:@"message=%@",@"hello,world."];NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease]; [request setURL:[NSURL URLWithString:@"http://192.168.10.220:18080/data/1.jsp"]]; [request setHTTPMethod:@"POST"]; [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; //[NSURLConnection connectionWithRequest:request delegate:self ]; //同步请求的的代码//returnData就是返回得到的数据NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningRequest:nil error:nil];[post release]; 二、web服务器端代码<%String message = request.getParameter("message");System.out.println("message="+message);out.println("message="+message);%>采用json 格式post 字符串static NSString *urlString = @"http://192.168.1.103/WebAccess/admin/tablesSync.aspx"; //static NSString *urlString = @"http://www.google.cn"; NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]]; NSString *httpBodyString = @"a test string"; NSMutableArray *arrayData = [[NSMutableArray alloc] init]; NSMutableDictionary *dlist = [[NSMutableDictionary alloc] init]; [dlist setObject:@"tblArea" forKey:@"tableName"]; [dlist setObject:@"2009-10-24 11:20:00" forKey:@"lastChangeDate"]; [arrayData addObject:dlist]; //[request setHTTPBody:[httpBodyString dataUsingEncoding:NSUTF8StringEncoding]]; // //post = [[NSString alloc] initWithFormat:@"message=%@",@"hello,world."]; httpBodyString = [arrayData JSONRepresentation]; NSData *postData = [httpBodyString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; [request setHTTPBody:postData]; [request setHTTPMethod:@"POST"]; // NSURLResponse *reponse; NSError *error = nil; // NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&reponse error:&error]; if (error) { NSLog(@"Something wrong: %@",[error description]); }else { if (responseData) { NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; NSLog(@"get %@",responseString); } }NSMutableDictionary *dgetData = [responseString JSONValue];NSLog(@"dgetData: %@" , [dgetData description]);[dlist release];[arrayData release];
- iOS http request sample code
- ios sample code
- Sample code collection for iOS
- http request status code详解
- sample code
- IOS中发http request
- IOS开发苹果官方Sample Code及下载地址
- IOS开发苹果官方Sample Code及下载地址
- IOS开发苹果官方Sample Code及下载地址
- The blog about Sample Browser_Serach,download,learn,share, and request code samples
- JDK 1.5 Sample Code
- Events Sample Code(1)
- Events Sample Code(2)
- logic font sample code
- MetaWeblogAPI C# Code Sample
- JavaScript Sample Code
- XMl - xerces code sample
- java concurrent code sample
- freebsd sudo配置使用
- WINDOWS服务器安全设置
- 单片机C语言程序结构
- unrecognized/unsupported machine ID (r1 = 0x31f07d30).
- tomcat 用户配置
- iOS http request sample code
- VC助手破解,含VC2010
- 最老程序员创业札记:全文检索、数据挖掘、推荐引擎应用26
- 单片机c语言下lcd多级菜单的一种实现方法
- JS检测用户是否已安装.net framework
- 用C语言编写Socket程序
- 与垃圾回收相关的新API:System.pauseForGCIfCollectionImminent()
- Android 开发中的网络下载策略 与 文件缓存策略
- 苹果和微软:你更钟情于哪种用户界面?