今年國慶和中秋一塊兒放,雖然很歡快,可是沒有票了!!!html
因而本身倒騰了一個查詢餘票的小程序。java
一、先打開12306的頁面ajax
二、而後右鍵檢查,點networkjson
三、再點一下12306頁面上的查詢,就能夠看到發起了ajax請求小程序
四、點第一個,很明顯是json串,這樣就方便不少服務器
五、複製第二個的連接session
好比我這裏就是:app
https://kyfw.12306.cn/otn/leftTicket/queryX?leftTicketDTO.train_date=2017-10-01&leftTicketDTO.from_station=BJP&leftTicketDTO.to_station=NJH&purpose_codes=ADULTdom
這是經過HttpURLConnection來發起一個請求,裏面的網址就填準備工做裏面複製的那串ide
/** * 發起一個http請求 */ public static void sendHttp(){ URL url; int responsecode; HttpURLConnection urlConnection; BufferedReader reader; String line; try{ //忽略Ssl(針對12306) SslUtils.ignoreSsl(); //生成一個URL對象 url=new URL("這裏填你要訪問的網址"); /** * 這是爲了防止12306對同一ip屢次訪問進行限制 * 這裏填的ip是暫時有效的,想要獲取更多就得本身去找 搜索代理ip */ System.getProperties().setProperty("proxySet", "true"); System.setProperty("http.proxyHost", "120.78.15.63"); System.setProperty("http.proxyPort", "80"); //打開URL urlConnection = (HttpURLConnection)url.openConnection(); //僞造一個請求頭 通常網頁不用,有些網站會看你有沒有請求頭,好比 12306...... urlConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:55.0) Gecko/20100101 Firefox/55.0"); urlConnection.setRequestProperty("Host","kyfw.12306.cn"); urlConnection.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); urlConnection.setRequestProperty("Accept-Language","zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3"); urlConnection.setRequestProperty("Accept-Encoding","identity"); urlConnection.setRequestProperty("Connection","keep-alive"); urlConnection.setRequestProperty("Upgrade-Insecure-Requests","1"); //獲取服務器響應代碼 responsecode=urlConnection.getResponseCode(); //假如響應代碼爲200,就是表明成功 if(responsecode==200){ reader=new BufferedReader(new InputStreamReader(urlConnection.getInputStream(),"UTF-8")); while((line=reader.readLine())!=null){ System.out.println(line);//在這裏幹你想幹的事情 } }else{ System.out.println("獲取不到網頁的源碼,服務器響應代碼爲:"+responsecode); } }catch(Exception e){ System.out.println("獲取不到網頁的源碼,出現異常:"+e); } }
跑這個方法以前還須要用到一個類,是用來忽略12306的證書問題
package domain; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSession; import javax.net.ssl.TrustManager; import javax.net.ssl.X509TrustManager; public class SslUtils { public static void trustAllHttpsCertificates() throws Exception { TrustManager[] trustAllCerts = new TrustManager[1]; TrustManager tm = new miTM(); trustAllCerts[0] = tm; SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, null); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); } static class miTM implements TrustManager,X509TrustManager { public X509Certificate[] getAcceptedIssuers() { return null; } public boolean isServerTrusted(X509Certificate[] certs) { return true; } public boolean isClientTrusted(X509Certificate[] certs) { return true; } public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException { return; } public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException { return; } } public static void ignoreSsl() throws Exception{ HostnameVerifier hv = new HostnameVerifier() { public boolean verify(String urlHostName, SSLSession session) { return true; } }; trustAllHttpsCertificates(); HttpsURLConnection.setDefaultHostnameVerifier(hv); } }
而後跑一下這個方法,看到獲取到了json
就能夠對這串字符串隨心所欲了,嘿嘿嘿
通常查到餘票都是發郵件,不會JavaMail的能夠看我另外一篇博客,開箱即用。
轉載需標註原文地址!