1.首先工具类内部需要遵守URLSessionDataDelegate代理
2.实现代理方法代码如下:
func urlSession(_ session:URLSession, didReceive challenge:URLAuthenticationChallenge, completionHandler:@escaping (URLSession.AuthChallengeDisposition,URLCredential?) -> Void) {
guard challenge.protectionSpace.authenticationMethod =="NSURLAuthenticationMethodServerTrust"else {
return
}
let credential = URLCredential.init(trust: challenge.protectionSpace.serverTrust!)
completionHandler(.useCredential,credential)
}
3.get方法内部实现如下:
// MARK:- getHttps请求
func getHttpsWithPath(path:String,paras: Dictionary<String,Any>?,success:@escaping ((_ result:Any) -> ()),failure: @escaping ((_ error:Error) -> ())) {
var i =0
var address = path
iflet paras = paras {
for (key,value)in paras {
if i ==0 {
address +="?\(key)=\(value)"
}else {
address +="&\(key)=\(value)"
}
i += 1
}
}
let url =URL(string: address.addingPercentEncoding(withAllowedCharacters:CharacterSet.urlQueryAllowed)!)
print(url!)
let request =URLRequest.init(url: url!)
let session =URLSession.init(configuration:URLSessionConfiguration.default, delegate:self, delegateQueue: OperationQueue.main)
let dataTask = session.dataTask(with: request, completionHandler: {(data,respond,error)in
iflet data = data {
iflet result = try?JSONSerialization.jsonObject(with: data, options: .allowFragments){
DispatchQueue.main.async(execute: {
success(result)
})
}
}else {
DispatchQueue.main.async(execute: {
failure(error!)
})
}
})
dataTask.resume()
}
4.post方法内部实现如下:
// MARK:- postHttps请求
func postHttpsWithPath(path:String,paras: Dictionary<String,Any>?,success:@escaping ((_ result:Any) -> ()),failure: @escaping ((_ error:Error) -> ())) {
var i =0
var address:String = ""
iflet paras = paras {
for (key,value)in paras {
if i ==0 {
address +="\(key)=\(value)"
}else {
address +="&\(key)=\(value)"
}
i += 1
}
}
let url =URL(string: path)
var request =URLRequest.init(url: url!)
request.httpMethod ="POST"
request.httpBody = address.data(using: .utf8)
let session =URLSession.init(configuration:URLSessionConfiguration.default, delegate:self, delegateQueue: OperationQueue.main)
let dataTask = session.dataTask(with: request) { (data, respond, error)in
iflet data = data {
iflet result = try?JSONSerialization.jsonObject(with: data, options: .allowFragments) {
DispatchQueue.main.async(execute: {
success(result)
})
}
}else {
DispatchQueue.main.async(execute: {
failure(error!)
})
}
}
dataTask.resume()
}
5.给工具类(HCTool)设置单例如下:
static let share =HCTool()
6.如何使用post工具类:(定义好路径path和参数dict即可,可传Nil)
let path ="https://10.148.83.228:9876/user/login/user/post/,/post"
let dict = ["phoneNum":"13025672622","password":"111111"]
HCTool.share.postHttpsWithPath(path: path, paras: dict, success: {(result) in
print(result)
}, failure: {(error) in
print(error)
})
7.如何使用get工具类(定义好path喝dict两个参数即可,可传Nil)
let path ="https://10.148.83.228:9876/user/login/user/post/,/post"
let dict = ["phoneNum":"13025672622","password":"111111"]
HCTool.share.getHttpsWithPath(path: path, paras: dict, success: {(result) in
print(result)
}, failure: {(error) in
print(error)
})
8.输出结果如下,正常:
{
description = "\U767b\U5f55\U6210\U529f\Uff01";
result = "S_OK";
tagObject = {
birth = "
email = "
expProIds = (
);
hobby = "
job = "
lat = 0;
locate = "
lon = 0;
phoneNum = 13025672622;
pwd = "";
qq = "
selfProIds = (
);
sex = 0;
userId = "b22f0020-17db-49b9-82e3-04619da1adec";
username = "\U9648\U5927\U7237";
verifyCode = "";
verifyTime = "
wechat = "
weibo = "
};
}
10.工具类下载地址:
http://download.csdn.net/detail/aa31140105/9709453