https request error block: mixed-content solution (resolved)

Problem screenshot

 

problem analysis

Today, a block: mixed-content  error suddenly appeared in multiple interfaces  , so I checked and found:

Error: https page to send http request error (browser prevents https from sending http request)

It turned out that because the project was changed to the https protocol, the request was intercepted;

In fact, the browser does not allow the request to embed http in the https page. Now the high-level browsers will not report errors for the user experience and will only print an error message on the console.

Mixed Content: The page was loaded over HTTPS,blocked the content must be served over HTTPS

As shown below:

Solution

Method 1.  Add the following code to the head of the main page ( upgrade the called http request to https request and call ):

<meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">

 

Method 2.  Take a look at the local backend (taking the local backend as the service middle layer), and then call the http requests of other servers from the backend

The following is the back-end transit interface

	/**
	 * 删除应用App
	 */
	@RequestMapping(value = "/delVersionConfig", produces = "text/html;charset=UTF-8")
	@ResponseBody
	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);
                // 调用http封装类
		String result = HttpUtils.getHttpClient(JSONURL, condMap);
		LOGGER.info("selectAllApplication_info--result:" + result);
		resultJSON = JSON.parseObject(result);
		return resultJSON;

	}

HttpUtils.getHttpClient execution method in HttpUtils package class

	/**
     * 向指定URL发送GET方法的请求
     *
     * @param url   发送请求的URL 例如:http://localhost:8080/demo/login
     * @param param 请求参数 例:{ "userName":"admin", "password":"123456" }
     * @return URL 所代表远程资源的响应结果
     */
    public static String getHttpClient(String url, Map<String, Object> param) {
        StringBuilder result = new StringBuilder();
        BufferedReader in = null;
        try {
            URL realUrl = createURL(url, param);
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Charset", "UTF-8");
            conn.connect();
            in = new BufferedReader(new InputStreamReader(
                    conn.getInputStream(),"UTF-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result.toString();
    }

 

Published 93 original articles · Like 1169 · Visit 250,000+

Guess you like

Origin blog.csdn.net/qq_39390545/article/details/105550949