问题
In our WKWebView, we have a multi part form POST request which we need to inspect and conditionally handle.
Currently, we're trying this using the WKNavigationDelegate's webView:decidePolicyForNavigationAction:decisionHandler:
method to gain access to the NSURLRequest. (navigationAction.request
).
But when we inspect the request here, we can verify that it is the multi part form POST, however, the [request HTTPBody]
returns nil
.
回答1:
Unfortunately it is the bug in WebKit :(( :
- https://bugs.webkit.org/show_bug.cgi?id=140188
- https://bugs.webkit.org/show_bug.cgi?id=145410
For some cases mentioned workaround from Florent Crivello can be used (https://bugs.webkit.org/show_bug.cgi?id=145410#c14):
NSString *javascriptPOSTRedirect = @"\
var form = document.createElement('form');\
form.method = 'POST';\
form.action = '<URL>';\
\
var input = document.createElement('input');\
input.type = 'text';\
input.name = '<key>';\
input.value = '<value>';\
form.appendChild(input);\
form.submit();";
[webView evaluateJavaScript:javascriptPOSTRedirect completionHandler:^(id _Nullable content, NSError * _Nullable error) {
// Your thing
}];
回答2:
Althrough i didn't found a docu for this, my shot would be that for security reasons the body in the request is empty, or assigned later than in
webView:decidePolicyForNavigationAction:decisionHandler:
回答3:
The request might contain a body stream. If so, and if you can modify the URL request, you could potentially read from that stream, then replace the request with a new one that uses a body data object.
Failing that, I think the only way to handle it would be to register a custom protocol handler that checks to see if it needs to handle it, and if so, handles it, and if not, either refuses it (if you can detect it without reading from the stream) or reissues it with some sort of tag that you can recognize (to avoid your protocol handler touching it the next time).
来源:https://stackoverflow.com/questions/31881800/in-ios-how-to-read-the-multi-part-form-data-in-the-nsurlrequest-originating-fro