从目标C获取coldfusion中的POST值

时间:2011-07-05 21:31:16

标签: iphone objective-c coldfusion nsurlconnection

我正在尝试将字符串发布到iphone中的URL,该URL将由服务器检索。服务器端脚本在coldfusion中完成。我试图在coldfusion中获取数据并将值传递回iphone。我对coldfusion并不熟悉,并希望得到一些帮助。以下是iphone中的编码:

/ ** Iphone代码** /

NSString *post =[[NSString alloc] initWithString:@"Hello World"];
NSURL *url=[NSURL URLWithString:@"http://www.abc.com/test.cfm"];


NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];


NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

/ * Coldfusion代码* /

<html>
<head><title>Test</title></head>
<body>
<cfoutput>#form.post#</cfoutput>
</body>
</html>

我是否正确地做到了?

1 个答案:

答案 0 :(得分:1)

您可能需要在数据中指定表单变量的名称。尝试下面的post变量。我没有测试它应该工作。

NSString *post =[[NSString alloc] initWithString:@"post=Hello%20World"];