【问题标题】:Multiple Parameter for POST Method in iPhoneiPhone中POST方法的多个参数
【发布时间】:2011-03-04 10:06:26
【问题描述】:

我正在尝试使用 POST 将多个参数发送到特定的 url。但它没有正确发送参数。

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

【问题讨论】:

  • 由于斜杠,您应该对 post 参数进行 urlencode。
  • 那么错误是什么?你的日志在说什么?您希望该应用做什么,它做错了什么?

标签: iphone


【解决方案1】:

我在这里有一个带有动态联系表单 ViewController 的完整 POST 示例:

https://github.com/mikecheckDev/MDContactForm

【讨论】:

    猜你喜欢
    • 2018-01-24
    • 1970-01-01
    • 2016-03-30
    • 1970-01-01
    • 1970-01-01
    • 2016-09-03
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多