zoukankan      html  css  js  c++  java
  • NSMutableURLRequest,在POST方式下传递参数

    1. [代码][C/C++]代码     
        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];
    2. [代码]一、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]; 
    3. [代码]二、web服务器端代码     
    <%
    String message = request.getParameter("message");
    System.out.println("message="+message);
    out.println("message="+message);
    %>
    4. [代码]采用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]; http://www.huiyi8.com/moban/
     [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];

  • 相关阅读:
    JQ和Js获取span标签的内容
    JS获取子节点、父节点和兄弟节点的方法实例总结
    JS实现系统时间(自动)
    CSS font-family 属性
    网页中导入特殊字体@font-face属性详解
    ****HTML模板资源汇总
    ***XAMPP:报错 Unable to load dynamic library的解决方法
    2016年宜昌楼市将迎来史上最激烈一战
    北大资源重磅来宜--宜昌未来商业中心将诞生
    HTML5调用传感器的资料汇总
  • 原文地址:https://www.cnblogs.com/xkzy/p/3813406.html
Copyright © 2011-2022 走看看