In iOS, how to read the multi part form data in th

2019-06-24 15:01发布

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.

3条回答
姐就是有狂的资本
2楼-- · 2019-06-24 15:19

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).

查看更多
看我几分像从前
3楼-- · 2019-06-24 15:26

Unfortunately it is the bug in WebKit :(( :

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
}];
查看更多
Fickle 薄情
4楼-- · 2019-06-24 15:30

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:

查看更多
登录 后发表回答