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];

NSMutableURLRequest,在POST方式下传递参数

时间: 2024-07-29 03:07:54

NSMutableURLRequest,在POST方式下传递参数的相关文章

JAVA HttpURLConnection Post方式提交传递参数

转自: http://wang09si.blog.163.com/blog/static/1701718042013631104658130/ JAVA HttpURLConnection Post方式提交传递参数 package wzq.j2se; import java.io.BufferedReader;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStreamReader;im

Jmeter在非GUI环境下传递参数(命令行&amp;Jenkins配置)

通过cmd运行 jmeter -? 可以得到所有命令行选项(本文最后) 其中可以看到下面 -J 的使用 -J, --jmeterproperty <argument>=<value> Define additional JMeter properties 当通过JMeter属性的方法调用变量时,需要修改变量的传入方式.普通的定义变量是:${threadsUsers}的形式. 但是本方法是通过添加JMeter的属性,属性和参数是不同的,因此在调用中采用${__P(threadsUse

Struts2 注解方式@Result传递参数给其他Action

/** * 生成订单 */ @Action(value="add", className="orderAction", results={ @Result(type="redirectAction", params={"actionName", "view", "oid", "%{oid}"}), @Result(name="input", loc

Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数(转)

Fragment在Android3.0开始提供,并且在兼容包中也提供了Fragment特性的支持.Fragment的推出让我们编写和管理用户界面更快捷更方便了. 但当我们实例化自定义Fragment时,为什么官方推荐Fragment.setArguments(Bundle bundle)这种方式来传递参数,而不推荐通过构造方法直接来传递参数呢?为了弄清这个问题,我们可以做一个测试,分别测试下这两种方式的不同 首先,我们来测试下通过构造方法传递参数的情况 public class FramentT

Android开发之Fragment传递参数的几种方法

Fragment在Android3.0开始提供,并且在兼容包中也提供了Fragment特性的支持.Fragment的推出让我们编写和管理用户界面更快捷更方便了. 但当我们实例化自定义Fragment时,为什么官方推荐Fragment.setArguments(Bundle bundle)这种方式来传递参数,而不推荐通过构造方法直接来传递参数呢?为了弄清这个问题,我们可以做一个测试,分别测试下这两种方式的不同 首先,我们来测试下通过构造方法传递参数的情况 public class FramentT

Android解惑 - 为什么要用Fragment.setArguments(Bundle bundle)来传递参数

Fragment在Android3.0开始提供,并且在兼容包中也提供了Fragment特性的支持.Fragment的推出让我们编写和管理用户界面更快捷更方便了. 但当我们实例化自定义Fragment时,为什么官方推荐Fragment.setArguments(Bundle bundle)这种方式来传递参数,而不推荐通过构造方法直接来传递参数呢? 为了弄清这个问题,我们可以做一个测试,分别测试下这两种方式的不同 首先,我们来测试下通过构造方法传递参数的情况 1 public class Frame

linux命令行传递参数定期执行PHP文件

最近在做一个项目,需要在linux下传递参数定期执行PHP文件,网上查询资料,确实有相关资料,现整理如下: 1.linux执行PHP文件 #{PHP安装bin路径} {PHP文件路径} {需要参数1 参数2 参数3}#各参数之间,用空格分开#查看php安装bin路径 可执行which php命令 /usr/bin/php /local/www/index.php 111 222 333 2.接收参数 一般情况下,php接收命令行传递过来的参数,$argv保存所有参数组成的数组,$argc保存参数

请写出并说明如何在命令行下运行PHP脚本(写出两种方式)同时向PHP脚本传递参数

方法一: <?php if ($argc > 1){ print_r($argv); } ?> php.exe test.php welcome Ricky Feng 方法二: 还有一种这样的,提示用户输入,然后获取输入的参数.有点像C语言 <?php fwrite(STDOUT, "Enter your name: "); $name = trim(fgets(STDIN)); fwrite(STDOUT, "Hello, $name!")

如何在命令行下运行PHP脚本(写出两种方式)同时向PHP脚本传递参数?

腾讯 window下,假设php安装目录为c:\program files\php5\,那么使用命令窗口进入到该路径下,敲入php hello.php回车,则会执行当前路径下的hello.php文件, 如果要指向其他路径下php文件,可以在php 路径/hello.php ,这种形式称为CLI模式,我们平时通过浏览器看到的那种成为CGI模式,至于传递参数,php文件在cli模式下,直接通过在文件名称后面接参数,多个参数中间用空格隔开,在php文件里面是通过两个变量来获取参数的,一个是$argv,