需要完成post请求才能完成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 post请求的‘数据格式’看起来是怎样的卷曲和迅速。
发布于 2020-01-15 21:16:08
更新1
好的,在阅读了您的评论之后,这里是发生了什么,程序甚至在代码被执行之前就结束了。查看这一点的一个简单方法是在代码中添加一个断点。
您可以使用信号量阻止当前线程,并等待URL会话完成。
调度信号量是传统计数信号量的有效实现。只有在需要阻塞调用线程时,分派信号量才会向下调用内核。如果调用信号量不需要阻塞,则不进行内核调用。苹果
这段代码会做你想做的事。
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"
);
} }https://stackoverflow.com/questions/59759585
复制相似问题