我正在尝试发出POST请求,但似乎无法找出问题所在。我从服务器收到响应,但是我的电子邮件/密码对似乎没有被服务器正确发送/读取,并且告诉我不存在这样的帐户。
这是我的代码,包含在一个函数中(当用户按下我创建的“登录”按钮时调用该代码)。注意:变量“电子邮件”和“密码”设置为我也创建的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
。
关于objective-c - 在Objective-C iPad开发人员中发布,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12712861/