【发布时间】:2019-03-22 10:27:36
【问题描述】:
我有一个 NSURLProtocol 监听 UIWebView 上的 POST 请求。我尝试捕获 POST 参数并首先读取here,httpBody 始终为零,因为主体数据对象被转换为流式主体。
然后我使用以下扩展打开 HTTPBodyStream 对象并从中读取正文数据。
extension InputStream {
func readfully() -> Data {
var result = Data()
var buffer = [UInt8](repeating: 0, count: 4096)
open()
var amount = 0
repeat {
amount = read(&buffer, maxLength: buffer.count)
if amount > 0 {
result.append(buffer, count: amount)
}
} while amount > 0
close()
return result
}
}
问题是我从输入流中读取的 bodyData 也是 nil。在 MyUrlProtocol 中,我重写了以下方法。
override class func canInit(with request: URLRequest) -> Bool
if request.httpMethod == "POST" {
print(request.url?.absoluteString) //ok show correct POST url
let bodyData = request.httpBodyStream?.readfully() //nil
print(String(data: bodyData!, encoding: String.Encoding.utf8))
return true
}
return false
}
override class func canonicalRequest(for request: URLRequest) -> URLRequest {
return request
}
override func startLoading() {
let bodyData = self.request.httpBodyStream?.readfully() //nil
}
override func stopLoading() {
let bodyData = self.request.httpBodyStream?.readfully() //nil
}
为什么我的自定义 NSURLProtocol 中的 httpBodyStream 也是 Nil?
我可以在我的网络浏览器中使用网络开发工具正确查看同一 URL 的 POST 参数。
【问题讨论】:
-
检查是否在httpBody中设置?
-
再看你提到的答案。在 2018 年 1 月 18 日的评论中,作者解释了设置流代理以读取流所必须执行的步骤。
-
@SachinVas httpBody 也为零
-
@MikeTaverne 是的,这正是我在扩展中所做的,打开流并读取字节。这个扩展曾经可以工作,但我不知道为什么我不能再捕获 httpBodyStream 数据了
标签: ios swift uiwebview nsurlprotocol