我有一個服務器與一些API,我用這個API作爲客戶端。代碼如下:iPad 1「400 BadRequest」與sendAsynchronousRequest,所有在iPad上工作3
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://***/api/user"]];
req.HTTPMethod = @"put";
req.HTTPBody = data; // it is string data from json dictionary
[NSURLConnection sendAsynchronousRequest:req queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
NSString *userData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//...
}];
所有工作的iPad 3,但是當我試圖測試的iPad 1的代碼,有一個錯誤。更具體地說:我的服務器只返回JSON,即使有一些錯誤,所以我可以看到錯誤是在客戶端還是在服務器端。因此,通常情況下,用戶數據應該包含一些字符串詞典:
{ "auth": 1, "email":"[email protected]", "error": 0}
有時服務器會返回(如果我嘗試不正確的身份驗證數據發送給它):
{ "auth": 0, "email":"", "error": 400, "errorMessage":"Bad Request"}
,但我所得到的iPad 1是HTML字符串:
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/1.2.1</center>
</body>
</html>
我有點困惑,因爲有完全相同的選項,同樣的互聯網連接,但iPad的1和iPad 3的行爲是不同的。重要提示:iPad 1擁有iOS 5.1.1,iPad 3擁有6.1。應該有一個非常簡單的解決方案,但我無法弄清楚。我不確定iPad的型號是否確實很重要。另外,我想同步請求,並沒有工作太...
備註:根據http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html,HTTP方法區分大小寫,所以它應該是'@「PUT」'。 – 2013-03-18 16:14:52
@MartinR WOW工作!驚人!我幾乎爲此瘋狂。但是,對於我來說,奇怪的行爲是,我希望Cocoa能夠處理所有關於字母的工作。寫一個答案,我會接受它。 – medvedNick 2013-03-18 16:25:36