Moya 设置超时时间和请求头

   //TODO:超时时间设置

        let requestTimeoutClosure = { (endpoint:Endpoint, done: @escaping MoyaProvider.RequestResultClosure) in

            do{

                var request = try endpoint.urlRequest()

                request.timeoutInterval = 20 //设置请求超时时间

                done(.success(request))

            }catch{

                return

            }

        }

        //TODO: 设置请求头部信息

//        let provider = MoyaProvider(requestClosure: requestTimeoutClosure)

        let myEndpointClosure = {(target:LoginApi) -> Endpoint in

            

            let url = target.baseURL.appendingPathComponent(target.path).absoluteString

            let endpoint = Endpoint(

                url: url, sampleResponseClosure: {.networkResponse(200,target.sampleData)}, method: target.method, task: target.task, httpHeaderFields: target.headers

            )

            

            return endpoint.adding(newHTTPHeaderFields:  [

                "Content-Type" : "application/x-www-form-urlencoded",

                "ECP-COOKIE" : ""

                ])

                    }

   //初始化provider

        let provider = MoyaProvider(endpointClosure: myEndpointClosure, requestClosure: requestTimeoutClosure)

 

其实请求头也可以在创建moya相关类的heades直接写好,如下:

    public var headers: [String : String]? {

              return ["Accept":"application/json","Content-Type":"application/json; charset=utf-8","x-client-system":"ios","x-client-systemVersion":UIDevice.current.systemVersion,"x-client-appVersion":app_version]

    }

你可能感兴趣的:(iOS,swift,Moya)