需要执行发布请求才能完成oauth2.0身份验证。
它在卷曲时效果很好:
curl -i -X POST -H 'Content-Type: application/x-www-form-urlencoded' -d 'clientId=cgoxyBxM1KVO3pLm5J7VgDVxlP7_33BpPlPXeIaSmoLsTZq8DfyM1svTwi-SU7KJKBRN4V3mIsV7pNNEg610Xw' https://fmsauth.scania.com/auth/S2S4DA/ClientId2Challenge
我得到这种回应:
{"challenge":"_jEEFcI36cvfMac8BHG8R0iIp4g7I-t0-C9LKAjwl9Y"}
我很快尝试了同样的事情,但返回的响应为零:
import Foundation
let session = URLSession.shared
let url = URL(string: "https://fmsauth.scania.com/auth/S2S4DA/ClientId2Challenge")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let payload = Data("clientId=cgoxyBxM1KVO3pLm5J7VgDVxlP7_33BpPlPXeIaSmoLsTZq8DfyM1svTwi-SU7KJKBRN4V3mIsV7pNNEg610Xw".utf8)
let task = session.uploadTask(with: request, from: payload) { data, response, error in
if let data = data, let dataString = String(data: data, encoding: .utf8) {
print(dataString)
}
if let httpResponse = response as? HTTPURLResponse {
print(httpResponse.statusCode)
}
}
task.resume()
我认为有效载荷数据字符串可能存在问题。我不知道http帖子请求“数据格式”在卷曲和迅捷中是什么样子。
答案 0 :(得分:0)
更新1
好吧,在阅读您的评论之后,这是怎么回事,该程序在您的代码甚至没有执行之前就已结束。一种简单的查看方法是在代码中添加断点。
您可以使用信号量来阻止当前线程,并等待URL会话结束。
调度信号量是传统方法的有效实现 计数信号量。调度信号仅调用内核 当调用线程需要被阻塞时。如果调用信号量 不需要阻止,无需进行内核调用。 Apple
此代码将执行您想要的操作。
var sema = DispatchSemaphore( value: 0 )
let session = URLSession.shared
let url = URL(string: "https://fmsauth.scania.com/auth/S2S4DA/ClientId2Challenge")!
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
let payload = Data("clientId=cgoxyBxM1KVO3pLm5J7VgDVxlP7_33BpPlPXeIaSmoLsTZq8DfyM1svTwi-SU7KJKBRN4V3mIsV7pNNEg610Xw".utf8)
let task = session.uploadTask(with: request, from: payload) { data, response, error in
if let data = data, let dataString = String(data: data, encoding: .utf8) {
print(dataString)
sema.signal()
}
if let httpResponse = response as? HTTPURLResponse {
print(httpResponse.statusCode)
}
}
task.resume()
sema.wait()
第一篇文章
您的代码有效,我认为您需要启用传输。将此添加到info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>somethingthatshouldntworkyourdomain.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<false/>
</dict>
</dict>
</dict>
响应
Code 200
{"challenge":"eeABaKaf2ZdhPEVQ4ru2S58_j37VLetM3pryIP8uiXk"}
<NSHTTPURLResponse: 0x600003be73e0> { URL: https://fmsauth.scania.com/auth/S2S4DA/ClientId2Challenge } { Status Code: 200, Headers {
"Content-Encoding" = (
gzip
);
"Content-Length" = (
182
);
"Content-Type" = (
"application/json; charset=utf-8"
);
Date = (
"Wed, 15 Jan 2020 21:12:31 GMT"
);
Server = (
"Microsoft-IIS/8.5"
);
"Set-Cookie" = (
"BIGipServerfmsauth.scania.com_https_pool=!c+BZzlVtgeV0E7NCFLoANbUAp39TaIJT2kJgIPLs8cCAJ4R4UMhNbWVOiSnTd/Cx6OuLMGUfIpke3g==; path=/; Httponly; Secure"
);
Vary = (
"Accept-Encoding"
);
"X-HandlingWebServer" = (
sesofms9112
);
"X-Powered-By" = (
"ASP.NET"
);
"owin.RequestId" = (
"4dbeb043-0e55-4972-b955-45c28f94aa77"
);
} }