iPhone中POST方法的多参数

时间:2011-03-04 10:06:26

标签: iphone

我正在尝试使用POST向特定网址发送多个参数。但它没有正确发送参数。

NSDate *now = [ NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/YY HH:mm:ss"];
NSString *startDateTime = [formatter stringFromDate:now];


NSMutableString *URL=[[[NSMutableString alloc] initWithString:LIVE_CHAT_URL] autorelease];
NSMutableURLRequest *request =  [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];

NSString *postData = [[NSString alloc] initWithFormat:@"Message=%@&MethodName=AddMessage&ToUserLoginId=18&StartTime=%@&Language=en&userID=%d",field.text,startDateTime,1];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSLog(@"post:%@",postData);  

[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];

[postData release];
[postLength release];
[formatter release];

NSError *error;
NSURLResponse *response;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];

1 个答案:

答案 0 :(得分:0)

我有一个完整的POST示例,其中包含动态联系人表单ViewController:

https://github.com/mikecheckDev/MDContactForm