Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用会话生成HTTP POST请求_Ios_Objective C_Swift - Fatal编程技术网

Ios 使用会话生成HTTP POST请求

Ios 使用会话生成HTTP POST请求,ios,objective-c,swift,Ios,Objective C,Swift,我尝试将POST请求发送到后端服务器,并将其用于objective-c中“ServiceUtils”类内的基本会话 (AFHTTPSessionManager *)baseSessionManager { if (httpSessionManager) { return httpSessionManager; } httpSessionManager = [AFHTTPSessionManager manager];

我尝试将POST请求发送到后端服务器,并将其用于objective-c中“ServiceUtils”类内的基本会话

(AFHTTPSessionManager *)baseSessionManager {

        if (httpSessionManager) {
            return httpSessionManager;
        }

        httpSessionManager = [AFHTTPSessionManager manager];
        httpSessionManager.requestSerializer = [AFJSONRequestSerializer serializer];
        [httpSessionManager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [httpSessionManager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

        // User agent for iOS app.
        NSString *userAgent = [NSString stringWithFormat:@"xxxxx Mobile iOS/%@ (%@ - %@ v%@)", [Utils appVersionWithBuild], [[UIDevice currentDevice] model], [[UIDevice currentDevice] systemName], [[UIDevice currentDevice] systemVersion]];

        [httpSessionManager.requestSerializer setValue:userAgent forHTTPHeaderField:@"User-Agent"];

        // Mobile auth 
        User *user = [User getUserWithContext:[NSManagedObjectContext MR_defaultContext]];
        NSString *mobileAuth = [[NSString stringWithFormat:@"%@--givemymoneybackdude", user.token] sha1];
        [httpSessionManager.requestSerializer setValue:mobileAuth forHTTPHeaderField:@"Mobile-Auth"];

        // Authorization
        NSString *authorization = [[NSString stringWithFormat:@"%@:X", user.token] stringBase64];
        [httpSessionManager.requestSerializer setValue:[NSString stringWithFormat:@"Basic %@", authorization] forHTTPHeaderField:@"Authorization"];

        // Locale
        if (user.locale) {
            [httpSessionManager.requestSerializer setValue:user.locale forHTTPHeaderField:@"Accept-Language"];
        }

        return httpSessionManager;
    }
这是我在swift 2.3中尝试发出POST请求的代码

if let url = NSURL(string: "xxxxxxxx") {
            let session = ServicesUtils.baseSessionManager()
            let request = NSMutableURLRequest(URL: url)

            request.HTTPMethod = "POST"
            request.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")

            let jsonObject = ["token" : "Token String", "refresh_token" : "refresh_token String","expires_in" : "expires_in String", "user_id" : "uber_uuid", "token_type" : "Bearer"]


            request.HTTPBody = try? NSJSONSerialization.dataWithJSONObject(jsonObject, options: [])

            session.dataTaskWithRequest(request) { data, response, error in
                guard
                 error == nil &&
                        (response as? NSHTTPURLResponse)?.statusCode == 200
                    else {
                        print((response as? NSHTTPURLResponse)?.statusCode ?? "no status code")
                        print(error?.localizedDescription ?? "no error description")
                        return
                }
                }.resume()
尝试此操作后,我出现以下错误:

请求失败:未经授权(401)


我检查了标题,它是正常的,但这个问题正在发生。有人能告诉我为什么会这样吗?我怎样才能将一个名为“集成”的字典传递到一个jsonObject中,其中包含了其中的值这段代码将帮助您…并使用Alamofire

    Alamofire.request(requestUrl, method: .post, parameters: (parametersDictionary as NSDictionary) as? Parameters , encoding: JSONEncoding.default, headers: nil).responseJSON { response in

        if(response.result.error == nil){

            if((response.response?.statusCode)! < 500){

                if(response.response?.statusCode == 200){

                    if let JSON = response.result.value {

                        let dict = JSON as! NSDictionary
                        let status :Bool = dict["status"] as! Bool

                        if(status){
                            success(dict)
                        }else{
                            failure(dict["message"] as! String)
                        }
                    }
                }else{
                    failure("Something went wrong please try again")
                }
            }else{
                 failure("Something went wrong please try again")
            }
        }
    }
}
Alamofire.request(requestUrl,方法:.post,参数:(parametersDictionary as NSDictionary)as?参数,编码:JSONEncoding.default,头:nil)。responseJSON{response in
if(response.result.error==nil){
如果((响应.响应?状态码)!<500){
if(response.response?.statusCode==200){
如果让JSON=response.result.value{
让dict=JSON作为!NSDictionary
让状态:Bool=dict[“状态”]as!Bool
如果(状态){
成功(格言)
}否则{
失败(dict[“message”]作为!字符串)
}
}
}否则{
失败(“出现问题,请重试”)
}
}否则{
失败(“出现问题,请重试”)
}
}
}
}

我希望以下链接可以帮助您:

为什么要在json数据中发送令牌?它应该作为头值传递。但这不是一个解决方案,而是一个建议。@TusharSharma due来自Uber的一次集成,集成完成后,我提取信息并将其发送到后端。因此,您也可以获得任何身份验证令牌或其他东西?@TusharSharma Yes您是否将该令牌作为头发送?