ios - Afnetworking multipart/formdata 返回请求在 ios 11 中失败,但在 ios 10 中工作正常

标签 ios swift afnetworking ios11

我正在尝试提交包含带有一些嵌套 JSON 的图像的多部分表单数据,但它在 ios 10 中工作正常,但对于 ios 11 返回请求失败: Not Acceptable 内容类型:text/html错误。 我使用的代码如下所示,如果我遗漏了什么,请告诉我。

//AFNetworking 多部分

func AFhttpPost(url:String,postData:NSMutableDictionary, completionHandler:(result: NSMutableDictionary,error:NSString) -> Void){

    print("postData \(postData)")

    var dataReceived:NSDictionary?
    var mutableData:NSMutableDictionary?
    if(self.checkNetConnectivity())
    {
        let afHTTP :AFJSONRequestSerializer = AFJSONRequestSerializer()


        afHTTP.setValue("multipart/form-data;", forHTTPHeaderField: "Content-Type")
        afHTTP.setValue("application/json", forHTTPHeaderField: "Accept")



        let request: NSMutableURLRequest = afHTTP.multipartFormRequestWithMethod("POST", URLString: url, parameters: nil, constructingBodyWithBlock: {(formData: AFMultipartFormData) in

            let allKeys:NSArray = postData.allKeys


            for item in allKeys.enumerate(){
                let currentKey = item.element as! String

                if(currentKey == "signature"){
                    let signatureImage = postData.objectForKey("signature") as! UIImage

                    let imageData = UIImagePNGRepresentation(signatureImage)!
                    formData.appendPartWithFileData(imageData , name: "uploaded_file[]", fileName: "signature\(index)x.png", mimeType: "image/png")


                }
                else
                {   //if it is jsonobject and its NSData just send as it is otherwise encode it
                    let postDataBytes:NSData?
                    if(postData.valueForKey(currentKey)! .isKindOfClass(NSData)){
                        postDataBytes = postData.valueForKey(currentKey)! as? NSData

                        print("postDataBytes: \(currentKey):\(postDataBytes)")

                    }else{
                        postDataBytes = postData.valueForKey(currentKey)!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion:true)!

                        print("postDataBytes: \(currentKey):\(postDataBytes)")

                    }
                    formData.appendPartWithFormData(postDataBytes!, name: currentKey)

                }

            }

            print("formFinalData \(formData)")


            }, error: nil)

        let accessToken  = SingleTon().getAccessToken()
        let authorisationheader  = "\(accessToken)"
        request.setValue(authorisationheader, forHTTPHeaderField: "Authorization")

        let config = NSURLSessionConfiguration.defaultSessionConfiguration()
        config.URLCache = nil
        config.URLCredentialStorage = nil
        // let session = NSURLSession(configuration: config)




        let managerS : AFURLSessionManager = AFURLSessionManager.init(sessionConfiguration:config)

        managerS.responseSerializer = AFJSONResponseSerializer()




        let uploadTask = managerS.uploadTaskWithStreamedRequest(request, progress: nil) { (response, responseObject, error) -> Void in

            do
            {  print("data \(responseObject)")
                if(responseObject != nil){
                    //dataReceived = try NSJSONSerialization.JSONObjectWithData(responseObject! as! NSData, options: []) as? NSMutableDictionary
                    dataReceived = responseObject! as? NSDictionary

                    mutableData = dataReceived!.mutableCopy() as? NSMutableDictionary
                    completionHandler(result:mutableData! ,error:"")

                }else{
                    completionHandler(result:[:],error:error!.localizedDescription)


                }

            }
        }
        uploadTask.resume()
    }else{

        completionHandler(result: [:],error:ReapConectivityStatus)
    }


}

最佳答案

这意味着您的服务器正在发送“text/html”而不是已经支持的类型。我的解决方案是将“text/html”添加到 AFURLResponseSerialization 类中设置的acceptedContentTypes 中。只需搜索“acceptableContentTypes”并手动将@“text/html”添加到集合中即可。

关于ios - Afnetworking multipart/formdata 返回请求在 ios 11 中失败,但在 ios 10 中工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47174047/

相关文章:

objective-c - 使用 Xcode 6 的 AFURLRequestSerialization 中的自动合成错误

json - Swift - 检查 JSON 是否有效

ios - 调用 didEnterRegion 时,didUpdateLocations 不再被调用

objective-c - AF网络进度方法

ios - 从普通 View Controller 打开 ECSlidingViewController

swift - 您可以在 Swift 中为特定的泛型类型进行协议(protocol)扩展吗?

ios - 如何使用 AFNetworking 管理 session ?

ios - 如何解压 zlib NSData

ios - 如何将数据放在 UITableViewCell 的右侧

objective-c - 多值设置 (Settings.bundle) 问题