2012-10-03 50 views
2

我試圖做一個POST請求,我似乎無法弄清楚什麼是錯誤的。我收到服務器的迴應,但我的電子郵件/密碼對似乎沒有被正確發送/讀取(由服務器),它告訴我沒有這樣的帳戶存在。在Objective-C iPad中開機自檢

這裏是我的代碼包含一個函數中(當用戶按下「登錄」按鈕,我創建了被稱爲)。注意:變量'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]; 
} 

任何人都可以看到我的邏輯有什麼問題嗎?

回答

2

您正在添加一個Current-Type標頭(AFAIK甚至不是有效標頭)而不是Content-Type

+0

驚人!我刪除了第一個「Content-Type」行,並將Current-Type修改爲Content-Type和BINGO! 非常感謝! –

3

感謝「布賴恩」,他解決了我的問題......我修改了我的代碼,現在它正在工作。 下面是最終代碼:

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