首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >http请求意外跳转到https的原因?

http请求意外跳转到https的原因?
EN

Stack Overflow用户
提问于 2021-05-27 11:22:02
回答 1查看 29关注 0票数 0

我正在使用WKWebView发送一些http请求。使用代码很简单,如下所示:

代码语言:javascript
复制
WKWebViewConfiguration* config = [[WKWebViewConfiguration alloc] init];
self.webView = [[WKWebView alloc] initWithFrame:webviewFrame configuration:config];

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]]];

但有时,一些http请求会被自动重定向到https。这些http请求的方法是POST,但在重定向后将更改为GET。同时,请求体中的数据被丢弃。

在重定向期间,委托方法webView:didReceiveServerRedirectForProvisionalNavigation:webView:decidePolicyForNavigationResponse:decisionHandlerwebView:didReceiveAuthenticationChallenge:completionHandler:都未被调用。只有webView:decidePolicyForNavigationAction:被调用了两次。

奇怪的是,一旦我将配置的websiteDataStore属性设置为nonPersistentDataStore,或者删除应用程序并重新安装,相同的请求就不会发生重定向。

代码语言:javascript
复制
WKWebViewConfiguration* config = [[WKWebViewConfiguration alloc] init];
config.websiteDataStore = [WKWebsiteDataStore nonPersistentDataStore];
self.webView = [[WKWebView alloc] initWithFrame:webviewFrame configuration:config];

[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:self.urlString]]];

但是,在我的项目中使用nonPersistentDataStore并不是一个好主意,因为有一些与登录相关的业务。

我想知道为什么会发生重定向,并且与websiteDataStore相关。

是否存在其他解决方案?

EN

回答 1

Stack Overflow用户

发布于 2021-05-31 10:36:48

最后我发现这是由于HSTS机制造成的。我的post请求被强制重定向为使用https,同时主体数据丢失。有人在Apple Developer fourms上发布了一个问题,但没有人回答。这是the link。有没有办法禁用我的WKWebView实例的这种机制?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67715241

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档