在Objective-C iPad开发中发布

我正在尝试发出POST请求,而我似乎无法弄清楚出了什么问题。 我收到了来自服务器的回复,但我的电子邮件/密码对似乎没有正确发送/读取(由服务器),它告诉我没有这样的帐户存在。

这是我的代码包含在一个函数中(当用户按下我创建的“登录”按钮时调用它)。 注意:变量’email’和’password’设置为我也创建的2个文本字段中的值。

NSURL *url = [NSURL URLWithString:@"http://mydomain.com/login-ipad/"]; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; NSString *messageBody = [NSString stringWithFormat:@"email=%@&user_pass=%@",email,password]; NSString *msgLength = [NSString stringWithFormat:@"%d", [messageBody length]]; [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; [theRequest setHTTPMethod:@"POST"]; [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; [theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"]; [theRequest setHTTPBody:[messageBody dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if(theConnection) { NSLog(@"Connection Successful"); parent.loginButton.enabled = NO; receivedData = [[NSMutableData data] retain]; } else { UIAlertView *alert1 = [[[UIAlertView alloc] initWithTitle:@"Alert" message:@"There was an issue sending the data. Please check your internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; [alert1 show]; } 

任何人都可以看到我的逻辑有什么问题?

您正在添加Current-Type标头(AFAIK甚至不是有效标头)而不是Content-Type

感谢“Brian”,他解决了我的问题…我修改了我的代码,现在正在运行。 这是最终的代码:

 NSURL *url = [NSURL URLWithString:@"http://mydomain.com/login-ipad/"]; NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; NSString *messageBody = [NSString stringWithFormat:@"email=%@&user_pass=%@",email,password]; NSString *msgLength = [NSString stringWithFormat:@"%d", [messageBody length]]; [theRequest setHTTPMethod:@"POST"]; [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; [theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; [theRequest setHTTPBody:[messageBody dataUsingEncoding:NSUTF8StringEncoding]]; NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self]; if(theConnection) { NSLog(@"Connection Successful"); parent.loginButton.enabled = NO; receivedData = [[NSMutableData data] retain]; } else { UIAlertView *alert1 = [[[UIAlertView alloc] initWithTitle:@"Alert" message:@"There was an issue sending the data. Please check your internet connection." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; [alert1 show]; }