Two days ago, I logged on to a website's resource page and found that my download resources had nearly 20 pages of resources to be commented on. Each download resource comment can have a resource score, the resource comment rules of a website can be commented once every 60 seconds. If you comment one by one, it takes a lot of time. Therefore, a website is developed to download resource comments, which automatically comments all resources that have not been commented on by me. The comment tool is as follows:
: Http://url.cn/Q9unBR
In the implementation process, the main difficulty is that the https protocol used for logon to a website requires a digital certificate.
Attach the core code:
1. HTTP request Interface
package suda.mingcai.csdn.http;public interface IHttpClient{String getPage(String url) throws Exception;String sendPost(String url, String param) throws Exception;}
2. HTTP request base class
package suda.mingcai.csdn.http;import java.net.CookieHandler;import java.net.CookieManager;import java.util.List;import suda.mingcai.csdn.https.HttpsClient;public abstract class HttpClient implements IHttpClient{public final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv:11.0) like Gecko";public final String ACCEPT = "text/html, application/xhtml+xml, */*";protected List<String> m_cookies;static{CookieHandler.setDefault(new CookieManager());}public static HttpClient createHttpRequest(String url){if(url.startsWith("https"))return new HttpsClient();elsereturn new HttpRequest();}public void setCookies(List<String> cookies){this.m_cookies = cookies;}public List<String> getCookies(){return this.m_cookies;}public abstract String getPage(String url) throws Exception;public abstract String sendPost(String url, String param) throws Exception;}
3. HTTPS Request
package suda.mingcai.csdn.https;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.URL;import javax.net.ssl.HttpsURLConnection;import suda.mingcai.csdn.http.HttpClient;public class HttpsClient extends HttpClient{private HttpsURLConnection m_conn;@Overridepublic String getPage(String url) throws Exception{URL u = new URL(url);m_conn = (HttpsURLConnection) u.openConnection();m_conn.setRequestMethod("GET");m_conn.setUseCaches(false);//set request headersm_conn.setRequestProperty("User-Agent", USER_AGENT);m_conn.setRequestProperty("Accept", ACCEPT);m_conn.setRequestProperty("Accept-Lauguage", "zh-CN,zh;q=0.8,en;q=0.6");//set cookiesif (m_cookies != null){String cookies = "";for (String cookie : m_cookies){cookies += ";" + cookie;}m_conn.addRequestProperty("Cookie", cookies);}//get responseBufferedReader reader = new BufferedReader(new InputStreamReader(m_conn.getInputStream()));StringBuffer response = new StringBuffer();String line;while((line = reader.readLine()) != null)response.append(line);m_cookies = m_conn.getHeaderFields().get("Set-Cookie");reader.close();return response.toString();}public String sendPost(String url, String params) throws Exception{StringBuilder builder = new StringBuilder();URL u = new URL(url);m_conn = (HttpsURLConnection) u.openConnection();m_conn.setUseCaches(false);m_conn.setRequestMethod("POST");m_conn.setRequestProperty("User-Agent", USER_AGENT);m_conn.setRequestProperty("Accept", ACCEPT);m_conn.setRequestProperty("Accept-Lauguage", "zh-CN,zh;q=0.8,en;q=0.6");m_conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");m_conn.setRequestProperty("Referer", "https://passport.website.net/account/login");m_conn.setRequestProperty("Connection", "keep-alive");//set cookiesm_conn.setRequestProperty("Content-Length", params.length() + "");System.out.println("send cookies...");if(m_cookies != null){String cookies = "";for(String cookie : m_cookies){cookies += ";" + cookie;}m_conn.addRequestProperty("Cookie", cookies);}m_conn.setDoOutput(true);m_conn.setDoInput(true);PrintWriter out = new PrintWriter(m_conn.getOutputStream());out.print(params);out.flush();out.close();int code = m_conn.getResponseCode();if(code == 200){BufferedReader reader = new BufferedReader(new InputStreamReader(m_conn.getInputStream()));String line;while ((line = reader.readLine()) != null)builder.append(line);reader.close();System.out.println("login successfully!");}else if(code == 302){String redictUrl = m_conn.getHeaderFields().get("Location").get(0);builder.append(redictUrl);}m_cookies = m_conn.getHeaderFields().get("Set-Cookie");return builder.toString();}}
4. HTTP Request
package suda.mingcai.csdn.http;import java.io.BufferedReader;import java.io.InputStreamReader;import java.io.PrintWriter;import java.net.HttpURLConnection;import java.net.URL;public class HttpRequest extends HttpClient{private HttpURLConnection m_conn;@Overridepublic String getPage(String url) throws Exception{URL u = new URL(url);m_conn = (HttpURLConnection) u.openConnection();m_conn.setRequestMethod("GET");m_conn.setUseCaches(false);// set request headersm_conn.setRequestProperty("User-Agent", USER_AGENT);m_conn.setRequestProperty("Accept", ACCEPT);m_conn.setRequestProperty("Accept-Lauguage", "zh-CN,zh;q=0.8,en;q=0.6");//set cookiesif (m_cookies != null){String cookies = "";for (String cookie : m_cookies){cookies += ";" + cookie;}m_conn.addRequestProperty("Cookie", cookies);}// get responseBufferedReader reader = new BufferedReader(new InputStreamReader(m_conn.getInputStream()));StringBuffer response = new StringBuffer();String line;while ((line = reader.readLine()) != null)response.append(line);m_cookies = m_conn.getHeaderFields().get("Set-Cookie");reader.close();return response.toString();}public String sendPost(String url, String params) throws Exception{StringBuilder builder = new StringBuilder();URL u = new URL(url);m_conn = (HttpURLConnection) u.openConnection();m_conn.setUseCaches(false);m_conn.setRequestMethod("POST");m_conn.setRequestProperty("User-Agent", USER_AGENT);m_conn.setRequestProperty("Accept", ACCEPT);m_conn.setRequestProperty("Accept-Lauguage", "zh-CN,zh;q=0.8,en;q=0.6");m_conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");m_conn.setRequestProperty("Referer","https://passport.website.net/account/login");m_conn.setRequestProperty("Connection", "keep-alive");m_conn.setRequestProperty("Host", "passport.website.net");// set cookiesm_conn.setRequestProperty("Content-Length", params.length() + "");if (m_cookies != null){String cookies = "";for (String cookie : m_cookies){cookies += ";" + cookie;}m_conn.addRequestProperty("Cookie", cookies);}m_conn.setDoOutput(true);m_conn.setDoInput(true);PrintWriter out = new PrintWriter(m_conn.getOutputStream());out.print(params);out.flush();out.close();int code = m_conn.getResponseCode();if (code == 200){BufferedReader reader = new BufferedReader(new InputStreamReader(m_conn.getInputStream()));String line;while ((line = reader.readLine()) != null)builder.append(line);reader.close();}else if (code == 302){String redictUrl = m_conn.getHeaderFields().get("Location").get(0);builder.append(redictUrl);}m_cookies = m_conn.getHeaderFields().get("Set-Cookie");return builder.toString();}}
In view of the time relationship, the implementation principle is not too difficult. If you are interested, you can directly download the source code.