Last active
April 19, 2018 08:46
-
-
Save Jia-Hong-Peng/f559a885daaa4dbd318fe4798ad93e3a to your computer and use it in GitHub Desktop.
http post get
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
using System.Web.UI; | |
using System.Web.UI.WebControls; | |
using System.Net; | |
using System.Text; | |
using System.IO; | |
using System.Data.SqlClient; | |
using System.Data; | |
public partial class Update : Lotto.Data.PageBase | |
{ | |
static string url = "http://www.taiwanlottery.com.tw/Lotto/Lotto649/history.aspx"; | |
static string htmls = httpGet(url); //取得參數用 | |
static string htmlsNumber; //取得號碼用 | |
static string EVENTVALIDATION = getEVENTVALIDATION(); | |
static string VIEWSTATE = getVIEWSTATE(); | |
protected void Page_Load(object sender, EventArgs e) | |
{ | |
DateTime dt = DateTime.Now; | |
int tw_year = dt.Year - 1911; | |
for (int year = 96; year <= tw_year; year++) | |
{ | |
for (int month = 1; month <= 12; month++) | |
{ | |
NumberPeriod[] myNumberPeriod = getNumberPeriod(month, year); | |
if (InsertNum(myNumberPeriod)) | |
{ | |
Response.Write(year + "年" + month + "月,已儲存!"); | |
Response.Write("<br>"); | |
Response.Flush(); | |
} | |
else | |
{ | |
Response.Write(year + "年" + month + "月,無新資料寫入!"); | |
Response.Write("<br>"); | |
Response.Flush(); | |
} | |
} | |
} | |
//Response.Write(myNumberPeriod[0].number1); | |
//Response.Write("<br>"); | |
} | |
/// <summary> | |
/// 寫入遠端抓取的樂透號 | |
/// </summary> | |
/// <param name="myNumberPeriod"></param> | |
public bool InsertNum(NumberPeriod[] myNumberPeriod) | |
{ | |
bool result = false; | |
//寫入 | |
string sql = "insert into big_lotto( [no],[opendate],[closedate],[number],[number_1],[number_2],[number_3],[number_4],[number_5],[number_6],[number_sp]) values('@no','@opendate', '@closedate', '@number_all', '@number_1', '@number_2', '@number_3', '@number_4' , '@number_5', '@number_6', '@number_sp') "; | |
if (myNumberPeriod.Length > 0) | |
{ | |
foreach (NumberPeriod n in myNumberPeriod) | |
{ | |
//檢查是否存在 | |
string strSQL = "SELECT * FROM [big_lotto] where no = '" + n.no + "' "; | |
SqlDataAdapter adapter = new SqlDataAdapter(strSQL, (SqlConnection)connection); | |
DataSet ds = new DataSet(); | |
adapter.Fill(ds, "t"); | |
bool isExist = (ds.Tables["t"].Rows.Count > 0) ? true : false; | |
if (!isExist) | |
{ | |
string sql_tmp = sql; | |
sql_tmp = sql_tmp.Replace("@no", n.no); | |
sql_tmp = sql_tmp.Replace("@opendate", n.dateStart); | |
sql_tmp = sql_tmp.Replace("@closedate", n.dateClose); | |
sql_tmp = sql_tmp.Replace("@number_all", n.number1 + n.number2 + n.number3 + n.number4 + n.number5 + n.number6 + n.numberSP); | |
sql_tmp = sql_tmp.Replace("@number_1", n.number1); | |
sql_tmp = sql_tmp.Replace("@number_2", n.number2); | |
sql_tmp = sql_tmp.Replace("@number_3", n.number3); | |
sql_tmp = sql_tmp.Replace("@number_4", n.number4); | |
sql_tmp = sql_tmp.Replace("@number_5", n.number5); | |
sql_tmp = sql_tmp.Replace("@number_6", n.number6); | |
sql_tmp = sql_tmp.Replace("@number_sp", n.numberSP); | |
SqlCommand command = new SqlCommand(sql_tmp, (SqlConnection)connection); | |
command.ExecuteScalar(); | |
result = true; | |
} | |
} | |
} | |
return result; | |
} | |
/// <summary> | |
/// 取得每期樂透資料集合 | |
/// </summary> | |
/// <param name="month"></param> | |
/// <param name="year"></param> | |
/// <returns></returns> | |
public static NumberPeriod[] getNumberPeriod(int month, int year) | |
{ | |
htmlsNumber = httpPost(url, getParameters(month, year)); | |
string output = htmlsNumber; | |
output = getNumberMain(output); | |
string[] words = Split(output, "期別"); //分割每組 | |
NumberPeriod[] numberPeriod = new NumberPeriod[words.Length - 1]; //每期樂透資料 | |
for (int i = 1; i < words.Length; i++) //第0個是垃圾訊息 | |
{ | |
numberPeriod[i - 1] = new NumberPeriod(); | |
numberPeriod[i - 1].no = getNo(words[i]); | |
numberPeriod[i - 1].dateStart = getStart(words[i]); | |
numberPeriod[i - 1].dateClose = getClose(words[i]); | |
numberPeriod[i - 1].number1 = getNumber1(words[i]); | |
numberPeriod[i - 1].number2 = getNumber2(words[i]); | |
numberPeriod[i - 1].number3 = getNumber3(words[i]); | |
numberPeriod[i - 1].number4 = getNumber4(words[i]); | |
numberPeriod[i - 1].number5 = getNumber5(words[i]); | |
numberPeriod[i - 1].number6 = getNumber6(words[i]); | |
numberPeriod[i - 1].numberSP = getNumberSP(words[i]); | |
} | |
return numberPeriod; | |
} | |
/// <summary> | |
/// 取得號碼html主要部分 | |
/// </summary> | |
/// <param name="output"></param> | |
/// <returns></returns> | |
public static string getNumberMain(string output) | |
{ | |
output = GetInner(output, "月的中獎號碼", "註:"); | |
output = GetInner(output, "<table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">", " "); | |
output = output.Replace("<table align=\"center\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\">", ""); | |
output = Replace(output, "\r\n", ""); | |
output = Replace(output, " ", ""); | |
output = Replace(output, " ", ""); | |
return output; | |
} | |
public static string getNumberSP(string output) | |
{ | |
output = GetInner(output, "_SNo\">", "_No\">"); | |
output = GetInner(output, "_SNo\">", "</span>"); | |
output = Replace(output, "_SNo\">", ""); | |
return output; | |
} | |
public static string getNumber6(string output) | |
{ | |
output = GetInner(output, "_No6\">", "_SNo"); | |
output = GetInner(output, "_No6\">", "</span>"); | |
output = Replace(output, "_No6\">", ""); | |
return output; | |
} | |
public static string getNumber5(string output) | |
{ | |
output = GetInner(output, "_No5\">", "_No6"); | |
output = GetInner(output, "_No5\">", "</span>"); | |
output = Replace(output, "_No5\">", ""); | |
return output; | |
} | |
public static string getNumber4(string output) | |
{ | |
output = GetInner(output, "_No4\">", "_No5"); | |
output = GetInner(output, "_No4\">", "</span>"); | |
output = Replace(output, "_No4\">", ""); | |
return output; | |
} | |
public static string getNumber3(string output) | |
{ | |
output = GetInner(output, "_No3\">", "_No4"); | |
output = GetInner(output, "_No3\">", "</span>"); | |
output = Replace(output, "_No3\">", ""); | |
return output; | |
} | |
public static string getNumber2(string output) | |
{ | |
output = GetInner(output, "_No2\">", "_No3"); | |
output = GetInner(output, "_No2\">", "</span>"); | |
output = Replace(output, "_No2\">", ""); | |
return output; | |
} | |
public static string getNumber1(string output) | |
{ | |
output = GetInner(output, "_No1\">", "_No2"); | |
output = GetInner(output, "_No1\">", "</span>"); | |
output = Replace(output, "_No1\">", ""); | |
return output; | |
} | |
public static string getClose(string output) | |
{ | |
output = GetInner(output, "L649_EDate\">", "大小"); | |
output = GetInner(output, "L649_EDate\">", "</span>"); | |
output = Replace(output, "L649_EDate\">", ""); | |
return output; | |
} | |
public static string getStart(string output) | |
{ | |
output = GetInner(output, "L649_DDate\">", "開出"); | |
output = GetInner(output, "L649_DDate\">", "</span>"); | |
output = Replace(output, "L649_DDate\">", ""); | |
return output; | |
} | |
public static string getNo(string output) | |
{ | |
output = GetInner(output, "_DrawTerm\">", "開獎"); | |
output = GetInner(output, "_DrawTerm\">", "</span>"); | |
output = Replace(output, "_DrawTerm\">", ""); | |
return output; | |
} | |
public static string[] Split(string input, string target) | |
{ | |
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(target); | |
return regex.Split(input); | |
} | |
public static string Replace(string input, string target, string tostring) | |
{ | |
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(target); | |
return regex.Replace(input, tostring); | |
} | |
/// <summary> | |
/// 取得完整post參數 | |
/// </summary> | |
/// <param name="month">月份</param> | |
/// <param name="year">民國年</param> | |
/// <returns>整頁html</returns> | |
public static string getParameters(int month, int year) | |
{ | |
string type = "Lotto649Control_history1%24DropDownList1=2"; | |
string btn = "&Lotto649Control_history1%24btnSubmit=%E6%9F%A5%E8%A9%A2"; | |
string radYM = "&Lotto649Control_history1%24chk=radYM"; | |
string mmm = "&Lotto649Control_history1%24dropMonth=" + month; | |
string yyy = "&Lotto649Control_history1%24dropYear=" + year; | |
string other = "&__EVENTARGUMENT=&__EVENTTARGET="; | |
string key1 = "&__EVENTVALIDATION=" + HttpUtility.UrlEncode(EVENTVALIDATION); | |
string key2 = "&__LASTFOCUS=&__VIEWSTATE=" + HttpUtility.UrlEncode(VIEWSTATE); | |
string end = "&__VIEWSTATEENCRYPTED="; | |
return type + btn + radYM + mmm + yyy + other + key1 + key2 + end; | |
} | |
/// <summary> | |
/// 取得必要的post參數 | |
/// </summary> | |
/// <returns></returns> | |
public static string getEVENTVALIDATION() | |
{ | |
string output = htmls; | |
//抓取值 | |
output = GetInner(output, "__EVENTVALIDATION", "<table width=\"939\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" bgcolor=\"fff299\">"); | |
output = GetInner(output, "value=\"", "\" />"); | |
output = output.Replace("value=\"", ""); | |
return output; | |
} | |
/// <summary> | |
/// 取得必要的post參數 | |
/// </summary> | |
/// <returns></returns> | |
public static string getVIEWSTATE() | |
{ | |
string output = htmls; | |
//抓取值 | |
output = GetInner(output, "__VIEWSTATE", "__VIEWSTATEENCRYPTED"); | |
output = GetInner(output, "value=\"", "\" />"); | |
output = output.Replace("value=\"", ""); | |
return output; | |
} | |
/// <summary> | |
/// 取得中間值 | |
/// </summary> | |
/// <param name="htmls">內文</param> | |
/// <param name="ff">頭</param> | |
/// <param name="ll">尾</param> | |
/// <returns></returns> | |
public static string GetInner(string htmls, string ff, string ll) | |
{ | |
int first = htmls.IndexOf(ff); //關鍵字1 | |
int last = htmls.LastIndexOf(ll); //關鍵字2 | |
try | |
{ | |
string output = htmls.Substring(first, last - first); | |
return output; | |
} | |
catch (Exception e) | |
{ | |
return e.ToString(); | |
} | |
} | |
//接收網頁數據 | |
private static string httpGet(string URI) | |
{ | |
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URI); | |
//req.Proxy = new WebProxy(ProxyString, true); //true means no proxy | |
WebResponse resp = req.GetResponse(); | |
StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("UTF-8")); | |
string sReturn = sr.ReadToEnd().Trim(); | |
resp.Close(); sr.Close(); | |
return sReturn; | |
} | |
//向網頁提交數據並接收返回信息: | |
private static string httpPost(string URI, string Parameters) | |
{ | |
byte[] bytes = Encoding.Default.GetBytes(Parameters); | |
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URI); | |
req.CookieContainer = new CookieContainer(); | |
// req.Proxy = new WebProxy(ProxyString, true); | |
//req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.9) Gecko/2008052906 Firefox/3.0"; | |
//req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; | |
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.8) Gecko/20051111 Firefox/1.5"; | |
req.Accept = "text/xml,application/xml,application/xhtml+xml,text/html"; | |
req.Timeout = 10000; | |
req.KeepAlive = true; | |
req.Referer = URI; //修改為自己的referer | |
req.ContentType = "application/x-www-form-urlencoded"; | |
req.Method = "POST"; | |
req.ContentLength = bytes.Length; | |
Stream os = req.GetRequestStream(); | |
os.Write(bytes, 0, bytes.Length); //以上向伺服器post信息。 | |
os.Close(); | |
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();//以下獲取伺服器返回信息 | |
if (resp == null) return null; | |
StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.GetEncoding("UTF-8")); | |
string sReturn = sr.ReadToEnd().Trim(); | |
resp.Close(); sr.Close(); | |
return sReturn; | |
} | |
public class NumberPeriod | |
{ | |
public string no = ""; | |
public string dateStart = ""; | |
public string dateClose = ""; | |
public string number1 = ""; | |
public string number2 = ""; | |
public string number3 = ""; | |
public string number4 = ""; | |
public string number5 = ""; | |
public string number6 = ""; | |
public string numberSP = ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment