2014-03-13 72 views
12

我想我的代碼轉換爲AFNetworking 2.0分分級AFHTTPRequestOperationManager這裏是我的代碼AFNetworking 2.0域名= AFNetworkingErrorDomain代碼= -1011「請求失敗:內部服務器錯誤(500)

+ (NSAFNetwokingRequestManager *)sharedClient { 
    static NSAFNetwokingRequestManager *_sharedClient = nil; 

    static dispatch_once_t onceToken; 
    dispatch_once(&onceToken, ^{ 
     _sharedClient = [[self alloc] initWithBaseURL:[NSURL URLWithString:GET_CAR_BRAND]]; 
    }); 
    return _sharedClient; 
} 
- (instancetype)initWithBaseURL:(NSURL *)url 
{ 
    self = [super initWithBaseURL:url]; 
    if (self) { 
    self.responseSerializer = [AFXMLParserResponseSerializer serializer]; 
    self.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/soap+xml"]; 
    self.requestSerializer = [AFHTTPRequestSerializer serializer]; 
    [self.requestSerializer setValue:@"application/soap+xml" forHTTPHeaderField:@"Content-type"]; 
} 

    return self; 
} 
- (void)requestBrandcompletion:(NSAFNetwokingRequestManagerCompletionBlock)completion { 
    NSString *[email protected]"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" 
    "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" 
    "<soap:Body>\n" 
    " <CarBrandExt xmlns=\"http://www.nohausystems.nl/\" />\n" 
    "</soap:Body>\n" 
    "</soap:Envelope>\n"; 
    NSString *msgLength = [NSString stringWithFormat:@"%i",[soapRequest length]]; 
    [self POST:GET_CAR_BRAND parameters:Nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
      [formData appendPartWithHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"text/xml; charset=utf-8", @"Content-Type", msgLength, @"Content-Length", nil] body:[soapRequest dataUsingEncoding:NSUTF8StringEncoding]]; 
    } success:^(AFHTTPRequestOperation *operation, id responseObject) { 
     if (completion) { 
      completion(YES, responseObject); 
     } 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
     if (completion) { 
      completion(NO, nil); 
      NSLog(@"Unable to fetch record error %@ with user info %@.", error, error.userInfo); 
     } 
    }]; 
} 

我獲取此錯誤域= AFNetworkingErrorDomain代碼= -1011「請求失敗:內部服務器錯誤(500)。任何人都可以告訴我我在這裏做錯了什麼? 聞聽此事的迴應:

{ status code: 500, headers { 
    "Cache-Control" = private; 
    "Content-Length" = 509; 
    "Content-Type" = "application/soap+xml; charset=utf-8"; 
    Date = "Thu, 13 Mar 2014 12:59:45 GMT"; 
    Server = "Microsoft-IIS/7.5"; 
    "X-AspNet-Version" = "2.0.50727"; 
    "X-Powered-By" = "ASP.NET"; 
} } 
+0

不看你的代碼,錯誤500服務器錯誤,你看看你的服務器的日誌? –

+0

相同的Web服務工作正常與舊版AFNetworking –

+0

所以我想我的實現有一些問題。 –

回答

2

在你的代碼有:

[self POST:GET_CAR_BRAND parameters:Nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
      [formData appendPartWithHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"text/xml; charset=utf-8", @"Content-Type", msgLength, @"Content-Length", nil] body:[soapRequest dataUsingEncoding:NSUTF8StringEncoding]]; 

這defineds內容類型爲 「text/xml」 的,而服務器顯然是希望 「應用程序/肥皂+ XML」 。你應該嘗試改變代碼的那部分:

[self POST:GET_CAR_BRAND parameters:Nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
      [formData appendPartWithHeaders:[NSDictionary dictionaryWithObjectsAndKeys:@"application/sopa+xml; charset=utf-8", @"Content-Type", msgLength, @"Content-Length", nil] body:[soapRequest dataUsingEncoding:NSUTF8StringEncoding]]; 

更新建議:

嘗試增加:

[self.requestSerializer setValue:@"application/soap+xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
- (instancetype)initWithBaseURL:(NSURL *)url方法結束

如果這沒有幫助,我會建議做一些更詳細的網絡請求調試。你可以例如設置AFNetworkActivityLogger將請求/響應信息記錄到控制檯。

+0

我已經嘗試過這種改變也.. –

+0

我給了它更詳細的看看(我也很新到AFNetworking 2.0)並更新了答案。請嘗試,如果有效。 – lari

1

只是一個念頭,小心你正在使用BASEURLAFNetworking。如果您提供的確切端點網址爲AFNetworking,則可能會失敗。

對於如:

你有一個端點URL = http://myweb.com/api.php

在上述情況下,如果你正在使用AFNetworking,該BASEURL != URL,而應該像BASEURL=http://myweb.com/並同時呼籲任何要求供應URL = api.phpAFNetworking附加BASEURL + URL並創建一個完整的端點url。

雖然上述的答案可能不適合您的情況。但是,如果它有助於馴服其他問題,或者也可能是你的情況。

謝謝。

1

「請求失敗:不可接受的內容類型:application /肥皂+ xml」的由AFHTTPResponseSerializer的

- (BOOL)validateResponse:(NSHTTPURLResponse *)response 
        data:(NSData *)data 
        error:(NSError * __autoreleasing *)error 

方法在不可預料的MIME類型的響應的情況下產生的錯誤。

你可以用下面的代碼

self.responseSerializer = [AFXMLParserResponseSerializer serializer]; 
NSSet *set = self.responseSerializer.acceptableContentTypes; 
self.responseSerializer.acceptableContentTypes = [set setByAddingObject:@"application/soap+xml"]; 

或修改AFXMLDocumentResponseSerializer修復它 - 增加「應用程序/肥皂+ XML」到可接受的內容類型:

- (instancetype)init { 
    self = [super init]; 
    if (!self) { 
     return nil; 
    } 

    self.acceptableContentTypes = [[NSSet alloc] initWithObjects:@"application/xml", @"text/xml", @"application/soap+xml", nil]; 

    return self; 
} 

繼不會解決這個問題,我猜

[self.requestSerializer setValue:@"application/soap+xml" forHTTPHeaderField:@"Content-type"]; 

沒有足夠的信息來解釋d爲什麼你有「」請求失敗:內部服務器錯誤(500)「我會發布正確和不正確的請求,我們會盡力幫助你。

+0

我已經嘗試過,也看到編輯的問題。謝謝 –

+0

我不認爲'[self.requestSerializer setValue:@「application/soap + xml」forHTTPHeaderField:@「Content-type」];'可以解決您的任何問題。我已經更新了答案,請檢查。 – Avt

+0

謝謝解決我的應用程序/ soap + xml的問題,但仍然得到內部服務器錯誤.. –

1

試試這個代碼:

[self.requestSerializer setValue:@"application/soap+xml" forHTTPHeaderField:@"Accept"]; 
+0

我有同樣的問題,當我申請你的解決方案我的項目崩潰,給予「這個類是不是關鍵值編碼兼容的關鍵'接受'「。你有什麼想法/解決方案嗎?提前致謝。 –

相關問題