随笔 - 21  文章 - 2  评论 - 36  阅读 - 93260

NSMutableURLRequest,在POST方式下传递参数(转)

原文地址:http://www.cnblogs.com/anmog/archive/2011/03/09/1978621.html

    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];
posted on   心不蒙尘  阅读(7652)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
< 2012年2月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 1 2 3
4 5 6 7 8 9 10

点击右上角即可分享
微信分享提示