2

我正在尝试发出 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];
}

有人能看出我的逻辑有什么问题吗?

4

2 回答 2

3

感谢“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];
}
于 2012-10-03T16:52:26.817 回答
2

您正在添加一个Current-Type标题(AFAIK 甚至不是一个有效的标题)而不是Content-Type.

于 2012-10-03T16:42:41.513 回答