2014-02-13 73 views
1

我正在使用AFNetworking 2.1.0在我的iPad應用程序和我的服務器之間進行通信。AFNetworking 2.0:多部分POST時出錯503

我繼承AFHTTPSessionManager和使用沒有問題如下:

[self POST:kAPIPath 
parameters:params 
    success:^(NSURLSessionDataTask *task, id responseObject) { 
        successBlock(responseObject[@"result"]); 
       } 
    failure:^(NSURLSessionDataTask *task, NSError *error) { 
        failureBlock(error); 
       }]; 

但是當我添加了多部分,我的服務器返回

request failure. error: Request failed: service indisponible (503)

所以這不起作用:

[self POST:kAPIPath 
parameters:params 
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
       [formData appendPartWithFileData:uploadFile 
              name:@"file" 
             fileName:@"photo.jpg" 
             mimeType:@"image/jpeg"]; 
       } 
    success:^(NSURLSessionDataTask *task, id responseObject) { 
        successBlock(responseObject[@"result"]); 
       } 
    failure:^(NSURLSessionDataTask *task, NSError *error) { 
        failureBlock(error); 
       }]; 

但它也返回錯誤503,當我只是這樣做(沒有修改formData):

[self POST:kAPIPath 
parameters:params 
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 

       } 
    success:^(NSURLSessionDataTask *task, id responseObject) { 
        successBlock(responseObject[@"result"]); 
       } 
    failure:^(NSURLSessionDataTask *task, NSError *error) { 
        failureBlock(error); 
       }]; 

或本(零爲塊):

[self POST:kAPIPath 
parameters:params 
constructingBodyWithBlock:nil 
    success:^(NSURLSessionDataTask *task, id responseObject) { 
        successBlock(responseObject[@"result"]); 
       } 
    failure:^(NSURLSessionDataTask *task, NSError *error) { 
        failureBlock(error); 
       }]; 

我看了很多帖子,但不明白的地方這可能從何而來。錯誤503意味着

503 Service Unavailable The server is currently unavailable (because it is overloaded or down for maintenance).[2] Generally, this is a temporary state. Sometimes, this can be permanent as well on test servers.

但是,如果我使用沒有multipart的POST,我沒有問題。有沒有人有一個想法,這可能來自哪裏?

非常感謝!

+1

五百個系列錯誤在服務器端顯示出一個問題 –

回答

1

我解決了我的問題。

我認爲這是一個陳述和評論there相同的問題。它似乎是AFNetworking 2.0(最近)已知的多部分形式的問題:issue 1398。我在底部使用了Matt提出的解決方法,它正在工作。

感謝您的回答。

1

你是對的,使用多形式的方式是下面的方法。我建議你檢查你的服務器。嘗試使用Postman直接在瀏覽器中調試您的多種表單。

[self POST:kAPIPath 
parameters:params 
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
       [formData appendPartWithFileData:uploadFile 
              name:@"file" 
             fileName:@"photo.jpg" 
             mimeType:@"image/jpeg"]; 
       } 
    success:^(NSURLSessionDataTask *task, id responseObject) { 
        successBlock(responseObject[@"result"]); 
       } 
    failure:^(NSURLSessionDataTask *task, NSError *error) { 
        failureBlock(error); 
       }];