uts 写iOS端http请求使用URLSessionDelegate 复写的协议方法不生效,这个要怎么办?
class MySessionDelegate implements URLSessionDelegate {
urlSession(_ : URLSession, @argumentLabel("didReceive") challenge : URLAuthenticationChallenge, completionHandler : (disposition ?: URLSession.AuthChallengeDisposition, credential ?: URLCredential) => void) {
console.log("challenge-->", challenge);
if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
completionHandler(URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust = challenge.protectionSpace.serverTrust!))
} else {
completionHandler(URLSession.AuthChallengeDisposition.performDefaultHandling, null)
}
}
}
export const hkRequest : MyApi = function (params : MyApiOptions) {
const options : MyApiOptionsType = params.options
console.log("options-->", options);
const url = new URL(string = options.url)!
const request : URLRequest = new URLRequest(url = url)
request.httpMethod = options.method
if (options.method != 'GET') {
try {
request.httpBody = UTSiOS.try(JSONSerialization.data(withJSONObject = options.data, options = []))
} catch (e) {
//TODO handle the exception
params.fail?.(e)
return
}
}
request.setValue("application/json", forHTTPHeaderField = "Content-Type")
const session = new URLSession(configuration = URLSessionConfiguration.default, delegate = new MySessionDelegate(), delegateQueue = null)
try {
const task = session.dataTask(with = request, completionHandler = (data ?: Data, response ?: URLResponse, error ?: any) => {
console.log("response--->", response);
console.log("data-->", data);
console.log("error-->", JSON.stringify(error));
if (response == null) {
params.fail?.('请求异常')
return
}
params.complete?.(response)
if (error != null && data != null) {
params.fail?.(error)
return
}
console.log("data-->", data);
try {
const result = UTSiOS.try(JSONSerialization.jsonObject(with = data!))
params.success?.(result)
} catch (e) {
//TODO handle the exception
params.fail?.(e)
return
}
})
// 启动请求
task.resume()
} catch (e) {
//TODO handle the exception
console.log("e--->", e);
}
}
0 个回复