问题截图
问题分析
今天,多个接口突然出现 block:mixed-content 错误,于是排查了一下发现:
错误:https页面去发送http请求报错(浏览器阻止https发送http请求)
原来是由于项目改成了https协议的缘故,出现了请求被拦截;
其实是浏览器不允许在https页面里嵌入http的请求,现在高版本的浏览器为了用户体验,都不会弹窗报错,只会在控制台上打印一条错误信息。
Mixed Content: The page was loaded over HTTPS,blocked the content must be served over HTTPS
如下图:
解决方法
方法1. 在主页面的head中加入下面代码(将调用的http请求升级成https请求并调用):
<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
方法2. 走一下本地后端(将本地后端当成service中间层),从后端再去调用其他服务器的http请求
下面为后端中转接口
@RequestMapping(value = "/delVersionConfig", produces = "text/html;charset=UTF-8")
public JSONObject delVersionConfig(Model model, HttpServletRequest request, String appId) {
JSONObject resultJSON = new JSONObject();
String JSONURL = "http://" + CommonData.DATA_URL + "/app/xxx/xxxx";
Map<String, Object> condMap = new HashMap<String, Object>();
condMap.put("id", appId);
String result = HttpUtils.getHttpClient(JSONURL, condMap);
LOGGER.info("selectAllApplication_info--result:" + result);
resultJSON = JSON.parseObject(result);
HttpUtils封装类中HttpUtils.getHttpClient执行方法
public static String getHttpClient(String url, Map<String, Object> param) {
StringBuilder result = new StringBuilder();
BufferedReader in = null;
URL realUrl = createURL(url, param);
HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charset", "UTF-8");
in = new BufferedReader(new InputStreamReader(
conn.getInputStream(),"UTF-8"));
while ((line = in.readLine()) != null) {
System.out.println("发送GET请求出现异常!" + e);
return result.toString();