NSMutableURLRequest,在POST方式下传递参数

NSMutableURLRequest * request = [ [ NSMutableURLRequest alloc ] init ];
 
    NSUserDefaults *defs = [ NSUserDefaults standardUserDefaults ];     
      NSLog (@ "LoadcurrentCookie:%@", 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:NSASCIIStringEncodingallowLossyConversion:YES];   
NSString *postLength = [NSString stringWithFormat:@"%d", [postDatalength]];   
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init]autorelease];   
[request setURL:[NSURLURLWithString:@"http://192.168.10.220:18080/da ta/1.jsp"]];   
[request setHTTPMethod:@"POST"];
[request setValue:postLengthforHTTPHeaderField:@"Content-Length"];   
[request setValue:@"application/x-www-form-urlencoded"forHTTPHeaderField:@"Content-Type"];   
[requestsetHTTPBody:postData];   
//[NSURLConnection connectionWithRequest:request delegate:self];   

//同步请求的的代码
//returnData就是返回得到的数据
NSData *returnData = [NSURLConnectionsendSynchronousRequest:request returningRequest:nilerror: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:[httpBodyStringdataUsingEncoding:NSUTF8StringEncoding]];
//
    //post = [[NSString alloc]initWithFormat:@"message=%@",@"hello,world."];
httpBodyString = [arrayDataJSONRepresentation];
NSData *postData = [httpBodyStringdataUsingEncoding:NSASCIIStringEncodingallowLossyConversion:YES]; 
[request setHTTPBody:postData];
[request setHTTPMethod:@"POST"];
//
NSURLResponse *reponse;
NSError *error = nil;
//
NSData *responseData = [NSURLConnectionsendSynchronousRequest:requestreturningResponse:&reponseerror:&error];
if (error) {
  NSLog(@"Something wrong: %@",[errordescription]);
}else {
  if (responseData) {
    NSString *responseString =[[NSString alloc] initWithData:responseDataencoding:NSUTF8StringEncoding];
    NSLog(@"get%@",responseString);
  }
}


NSMutableDictionary *dgetData = [responseString JSONValue];
NSLog(@"dgetData: %@" , [dgetData description]);

[dlist release];
[arrayData release];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值