Sorting out the static knowledge points of the website

*. During the static development of the website, we also want to introduce html for the pages introduced in the system, so that when the imported pages change, all the referenced pages do not need to be static, but only need to be imported. You can make the page static.

Case: jquery dynamically loads html page load
http://www.360doc.com/content/15/0610/14/18139076_477138156.shtml

 

*, one of the core of website static: initiate a request to obtain the source code of the webpage

/**
 * Simulate a browser request, the given url returns the source code of the web page
 * @param urlPath web address
 * @param requestMethod GET or POST, the default is GET
 * @param codeType GBK or UTF-8, default is GBK
 */
public static String getHtmlCode(String urlPath , String requestMethod , String codeType) {
	String aimHtml = null;
	try {
		URL url=new URL(urlPath);
	HttpURLConnection httpConn=(HttpURLConnection)url.openConnection();
	
	//Setting parameters
	httpConn.setDoOutput(true); //Requires output
	httpConn.setDoInput(true); //Requires input
	httpConn.setUseCaches(false); //Do not allow caching
	httpConn.setRequestMethod(StringUtils.isBlank(requestMethod) ? "GET" : requestMethod); //Set GET connection
	//httpConn.setConnectTimeout(1000 * 10); //The connection times out for 10 seconds
	//httpConn.setReadTimeout(1000 * 6); //Read timeout 6 seconds
	// set request properties
	httpConn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
	httpConn.setRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
	httpConn.setRequestProperty("Accept-Language", "en-US,en;q=0.8");//The only place: key point key point key point
	httpConn.setRequestProperty("Connection", "Keep-Alive");
	httpConn.setRequestProperty("Charset", StringUtils.isBlank(codeType) ? "GBK" : codeType);
	httpConn.setRequestProperty("Cache-Control", "max-age=0");
	httpConn.setRequestProperty("Upgrade-Insecure-Requests", "1");
	httpConn.setRequestProperty("User-Agent", "Mozilla/5.0");
	httpConn.connect();
	
	// get response status
	int resultCode=httpConn.getResponseCode();
	if(HttpURLConnection.HTTP_OK==resultCode){
	    StringBuffer sb=new StringBuffer();
	    String readLine=new String();
	    BufferedReader responseReader=new BufferedReader(new InputStreamReader(httpConn.getInputStream(),StringUtils.isBlank(codeType) ? "GBK" : codeType));
	    while((readLine=responseReader.readLine())!=null){
		sb.append(readLine).append("\n");
	    }
	    responseReader.close();
	    aimHtml = sb.toString();
	}
	}catch(Exception e) {
		aimHtml = null;
		logger.error(urlPath+"URL access failed!",e);
	}
	return aimHtml;
}

 

*. The second core of the static website is to write the source code into the file without garbled characters

/**
 * Write the parsing result to the specified static HTML file
 *
 * @param pageContent
 * text content
 * @param fileUrl
 * target full path
 * @throws Exception
 */
private static void writeHtml(String pageContent, String fileUrl)
		throws Exception {
	try {
		OutputStreamWriter fw = new OutputStreamWriter(
				new FileOutputStream(fileUrl), "UTF-8");
		fw.write(pageContent);
		if (fw != null) {
			fw.close();
		}
	} catch (Exception e) {
		logger.error(fileUrl + "File writing failed!", e);
		throw e;
	}
}

 

*. The data structure + urlwriter will improve the efficiency when static, depending on the situation analysis, (*^__^*) hee hee...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326377953&siteId=291194637