HttpRequest 練習
- 取得連結
- X
- 以電子郵件傳送
- 其他應用程式
今天看到河道有人瘋狂轉 不囉唆!!與你最搭配的動漫女角!! 這個網頁
我無聊之下就想去試他倒底有幾個結果,目前全測出來大概是 68 個人物
另外這個動作其實就是種砍站的動作,是會造成伺服器負擔的,所以不要太常玩,要不然如果那網管會care的話,可能會ban 你的ip
那這裡主要還是在講解我的 HttpRequest 的練習,首先還是先丟出我的 HttpRequest.cs ,這是以前改的,那大多數時候我都直接套用這個 class 來進行 Request 的傳送及接收的動作
using System; using System.Collections.Generic; using System.IO; using System.Net; using System.Text; namespace WindowsFormsApplication2 { public partial class HttpRequest { public enum HttpVerbs { GET, POST } public static string MakeGetHttpRequest(string Url) { byte[] data = MakeHttpRequest(HttpVerbs.GET, Url, null, null); using (StreamReader reader = new StreamReader(new MemoryStream(data))) { string resultString = reader.ReadToEnd(); reader.Close(); return resultString; } } public static string MakeGetHttpRequest(string Url, Encoding encoding) { byte[] data = MakeHttpRequest(HttpVerbs.GET, Url, null, null); return encoding.GetString(data); } public static string PostRequest(string Url, string HttpData, Encoding encoding) { try { var result = MakeHttpRequest(HttpVerbs.POST, Url, null, encoding.GetBytes(HttpData)); return encoding.GetString(result); } catch { return ""; } } public static byte[] MakeHttpRequest(HttpVerbs Method, string Url, Dictionary<string, string> HttpHeaders, byte[] HttpData) { HttpWebRequest request = HttpWebRequest.Create(Url) as HttpWebRequest; HttpWebResponse response = null; byte[] resultData = null; ServicePointManager.Expect100Continue = false; request.Method = Method.ToString(); if (Method != HttpVerbs.GET) { request.ContentType = "application/x-www-form-urlencoded"; } if (HttpHeaders != null) { foreach (KeyValuePair<string, string> header in HttpHeaders) request.Headers.Add(header.Key, header.Value); } if (HttpData != null && HttpData.Length > 0) { Stream s = request.GetRequestStream(); s.Write(HttpData, 0, HttpData.Length); s.Close(); } try { response = request.GetResponse() as HttpWebResponse; MemoryStream ms = new MemoryStream(); BinaryReader br = new BinaryReader(response.GetResponseStream()); BinaryWriter bw = new BinaryWriter(ms); int bufferLength = 65536; int readedByteCount = 0; byte[] data = null; do { data = new byte[bufferLength]; readedByteCount = br.Read(data, 0, data.Length); if (readedByteCount == 0) break; else bw.Write(data, 0, readedByteCount); } while (readedByteCount <= bufferLength); br.Close(); bw.Flush(); ms.Position = 0; resultData = ms.ToArray(); bw.Close(); } catch (WebException we) { response = we.Response as HttpWebResponse; StreamReader sr = new StreamReader(response.GetResponseStream()); string responseData = sr.ReadToEnd(); sr.Close(); throw new HRNetworkException("ERROR_NETWORK_PROBLEM", request.RequestUri.ToString(), response.StatusCode, responseData); } catch (Exception e) { throw new HRException("ERROR_EXCEPION_OCCURRED", e); } finally { response.Close(); } response = null; request = null; return resultData; } public class HRNetworkException : Exception { public string Url { get; set; } public HttpStatusCode StatusCode { get; set; } public string ResponseDataString { get; set; } public HRNetworkException(string Message, string Url, HttpStatusCode StatusCode, string ResponseDataString) :base(Message) { this.Url = Url; this.StatusCode = StatusCode; this.ResponseDataString = ResponseDataString; } } public class HRException : Exception { public HRException() : base() { } public HRException(string message) : base(message) { } public HRException(string message, Exception innerException) : base(message, innerException) { } } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged); backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted); backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork); addItemToListView1 += AddItemToListView1; } public delegate void AddItem(int i, string Name); public AddItem addItemToListView1; public int TheNameCount = 0; public void AddItemToListView1(int i, string Name) { ListViewItem item = new ListViewItem(i.ToString()); Name = Name.Trim(); for (int j = 0; j < listView1.Items.Count; j++) { if (listView1.Items[j].SubItems[2].Text == Name) { item.SubItems.Add(listView1.Items[j].SubItems[1].Text); break; } } if (item.SubItems.Count == 1) { item.SubItems.Add((++TheNameCount).ToString()); } item.SubItems.Add(Name); listView1.Items.Add(item); } void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("『(.*)』"); TheValue theValue = e.Argument as TheValue; if (theValue != null) { for (int i = theValue.Minimum; i <= theValue.Maximum; i++) { var s = HttpRequest.PostRequest("http://t.weifadong.com/fortune_result.php?fortune_id=87", string.Format("user_name={0}", i), Encoding.GetEncoding("GBK")); var result = regex.Match(s); if (result.Success) { this.Invoke(addItemToListView1, i, result.Groups[1].Value); } backgroundWorker1.ReportProgress(i - theValue.Minimum); } } } void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { progressBar1.Visible = false; button1.Enabled = true; } void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { progressBar1.Value = e.ProgressPercentage; } private void button1_Click(object sender, EventArgs e) { TheValue theValue = new TheValue() { Minimum = Convert.ToInt32(textBox1.Text), Maximum = Convert.ToInt32(textBox2.Text) }; theValue.adjust(); progressBar1.Value = 0; progressBar1.Maximum = theValue.theSub + 1; progressBar1.Visible = true; button1.Enabled = false; backgroundWorker1.RunWorkerAsync(theValue); } public class TheValue { public int Minimum { get; set; } public int Maximum { get; set; } public int theSub { get { return Maximum - Minimum; } } public void adjust() { if (Minimum > Maximum) { int tmp = Minimum; Minimum = Maximum; Maximum = tmp; } } } private void button2_Click(object sender, EventArgs e) { if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK) { using (StreamWriter sw = new StreamWriter(saveFileDialog1.FileName)) { for (int i = 0; i < listView1.Items.Count; i++) { sw.WriteLine(string.Format("{0},{1},\"{2}\"", listView1.Items[i].SubItems[0].Text, listView1.Items[i].SubItems[1].Text, listView1.Items[i].SubItems[2].Text)); } sw.Close(); } } } } }
這隻程式主要使用到了 HttpWebRequest 、RegularExpressions 兩種主要工具
有興趣的可以自己研究看看XD
然後我跑了 1~500 共計 500 種的 結果 ,有興趣就自己抓下來看吧,就不要再砍一次了,減輕伺服器的負擔!
- 取得連結
- X
- 以電子郵件傳送
- 其他應用程式
留言
張貼留言