快速发布请求没有响应



需要执行 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"}

我在 swift 中尝试了同样的事情,但我得到的回复为零:

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 请求"数据格式"在 curl 和 swift 中是什么样子的。

更新 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"
);
} }

相关内容

  • 没有找到相关文章

最新更新