增加封IP检测...

master
j502647092 2015-07-30 18:54:16 +08:00
parent be29882c7d
commit be2b84f56b
4 changed files with 936 additions and 885 deletions

630
Frm_Main.Designer.cs generated
View File

@ -1,309 +1,321 @@
namespace EnAliKeywordSearch namespace EnAliKeywordSearch
{ {
partial class Frm_Main partial class Frm_Main
{ {
/// <summary> /// <summary>
/// 必需的设计器变量。 /// 必需的设计器变量。
/// </summary> /// </summary>
private System.ComponentModel.IContainer components = null; private System.ComponentModel.IContainer components = null;
/// <summary> /// <summary>
/// 清理所有正在使用的资源。 /// 清理所有正在使用的资源。
/// </summary> /// </summary>
/// <param name="disposing">如果应释放托管资源,为 true否则为 false。</param> /// <param name="disposing">如果应释放托管资源,为 true否则为 false。</param>
protected override void Dispose(bool disposing) protected override void Dispose(bool disposing)
{ {
if (disposing && (components != null)) if (disposing && (components != null))
{ {
components.Dispose(); components.Dispose();
} }
base.Dispose(disposing); base.Dispose(disposing);
} }
#region Windows 窗体设计器生成的代码 #region Windows 窗体设计器生成的代码
/// <summary> /// <summary>
/// 设计器支持所需的方法 - 不要 /// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。 /// 使用代码编辑器修改此方法的内容。
/// </summary> /// </summary>
private void InitializeComponent() private void InitializeComponent()
{ {
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle5 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle6 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle7 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle1 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle5 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle2 = new System.Windows.Forms.DataGridViewCellStyle();
System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle6 = new System.Windows.Forms.DataGridViewCellStyle(); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle3 = new System.Windows.Forms.DataGridViewCellStyle();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Frm_Main)); System.Windows.Forms.DataGridViewCellStyle dataGridViewCellStyle4 = new System.Windows.Forms.DataGridViewCellStyle();
this.SearchView = new System.Windows.Forms.DataGridView(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Frm_Main));
this.ResultView = new System.Windows.Forms.DataGridView(); this.SearchView = new System.Windows.Forms.DataGridView();
this.search = new System.Windows.Forms.Button(); this.ResultView = new System.Windows.Forms.DataGridView();
this.max = new System.Windows.Forms.TextBox(); this. = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.label1 = new System.Windows.Forms.Label(); this. = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.state = new System.Windows.Forms.Label(); this.r = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.saveout = new System.Windows.Forms.Button(); this.r = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.readin = new System.Windows.Forms.Button(); this.r = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.openFile = new System.Windows.Forms.OpenFileDialog(); this. = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.saveFile = new System.Windows.Forms.SaveFileDialog(); this.search = new System.Windows.Forms.Button();
this.clearresult = new System.Windows.Forms.Button(); this.max = new System.Windows.Forms.TextBox();
this.clearsearch = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label();
this.company = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.state = new System.Windows.Forms.Label();
this.s = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.saveout = new System.Windows.Forms.Button();
this.s = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.readin = new System.Windows.Forms.Button();
this.s = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.openFile = new System.Windows.Forms.OpenFileDialog();
this.keyword = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.saveFile = new System.Windows.Forms.SaveFileDialog();
this. = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.clearresult = new System.Windows.Forms.Button();
this. = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.clearsearch = new System.Windows.Forms.Button();
this.r = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.company = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.r = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.keyword = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.r = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.s = new System.Windows.Forms.DataGridViewTextBoxColumn();
this. = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.s = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.SearchView)).BeginInit(); this.s = new System.Windows.Forms.DataGridViewTextBoxColumn();
((System.ComponentModel.ISupportInitialize)(this.ResultView)).BeginInit(); this.sum = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.SearchView)).BeginInit();
// ((System.ComponentModel.ISupportInitialize)(this.ResultView)).BeginInit();
// SearchView this.SuspendLayout();
// //
this.SearchView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; // SearchView
this.SearchView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { //
this.company, this.SearchView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.s, this.SearchView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.s, this.company,
this.s, this.keyword,
this.keyword}); this.s,
this.SearchView.Location = new System.Drawing.Point(11, 58); this.s,
this.SearchView.Name = "SearchView"; this.s,
this.SearchView.RowTemplate.Height = 23; this.sum});
this.SearchView.Size = new System.Drawing.Size(850, 200); this.SearchView.Location = new System.Drawing.Point(10, 58);
this.SearchView.TabIndex = 0; this.SearchView.Name = "SearchView";
// this.SearchView.RowTemplate.Height = 23;
// ResultView this.SearchView.Size = new System.Drawing.Size(850, 200);
// this.SearchView.TabIndex = 0;
this.ResultView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; //
this.ResultView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { // ResultView
this., //
this., this.ResultView.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.r, this.ResultView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.r, this.,
this.r, this.,
this.}); this.r,
this.ResultView.Location = new System.Drawing.Point(11, 293); this.r,
this.ResultView.Name = "ResultView"; this.r,
this.ResultView.RowTemplate.Height = 23; this.});
this.ResultView.Size = new System.Drawing.Size(850, 300); this.ResultView.Location = new System.Drawing.Point(11, 293);
this.ResultView.TabIndex = 1; this.ResultView.Name = "ResultView";
// this.ResultView.RowTemplate.Height = 23;
// search this.ResultView.Size = new System.Drawing.Size(850, 300);
// this.ResultView.TabIndex = 1;
this.search.Location = new System.Drawing.Point(360, 10); //
this.search.Name = "search"; // 公司名称
this.search.Size = new System.Drawing.Size(152, 36); //
this.search.TabIndex = 2; this..HeaderText = "公司网站";
this.search.Text = "查 询 关 键 词"; this..Name = "公司名称";
this.search.UseVisualStyleBackColor = true; this..Width = 150;
this.search.Click += new System.EventHandler(this.search_Click); //
// // 关键词
// max //
// this..HeaderText = "关键词";
this.max.Location = new System.Drawing.Point(663, 10); this..Name = "关键词";
this.max.Name = "max"; this..Width = 200;
this.max.Size = new System.Drawing.Size(83, 21); //
this.max.TabIndex = 3; // 百度r
this.max.Text = "20"; //
// dataGridViewCellStyle5.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
// label1 this.r.DefaultCellStyle = dataGridViewCellStyle5;
// this.r.HeaderText = "百度";
this.label1.AutoSize = true; this.r.Name = "百度r";
this.label1.Location = new System.Drawing.Point(592, 13); //
this.label1.Name = "label1"; // 必应r
this.label1.Size = new System.Drawing.Size(65, 12); //
this.label1.TabIndex = 7; dataGridViewCellStyle6.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
this.label1.Text = "查询页数: "; this.r.DefaultCellStyle = dataGridViewCellStyle6;
// this.r.HeaderText = "必应";
// state this.r.Name = "必应r";
// //
this.state.AutoSize = true; // 搜狗r
this.state.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); //
this.state.ForeColor = System.Drawing.SystemColors.ControlText; dataGridViewCellStyle7.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
this.state.Location = new System.Drawing.Point(98, 267); this.r.DefaultCellStyle = dataGridViewCellStyle7;
this.state.Name = "state"; this.r.HeaderText = "搜狗";
this.state.Size = new System.Drawing.Size(37, 14); this.r.Name = "搜狗r";
this.state.TabIndex = 8; //
this.state.Text = "状态"; // 查询时间
this.state.TextAlign = System.Drawing.ContentAlignment.TopCenter; //
// this..HeaderText = "查询时间";
// saveout this..Name = "查询时间";
// this..Width = 130;
this.saveout.Location = new System.Drawing.Point(181, 29); //
this.saveout.Name = "saveout"; // search
this.saveout.Size = new System.Drawing.Size(73, 23); //
this.saveout.TabIndex = 9; this.search.Location = new System.Drawing.Point(360, 10);
this.saveout.Text = "导出关键词"; this.search.Name = "search";
this.saveout.UseVisualStyleBackColor = true; this.search.Size = new System.Drawing.Size(152, 36);
this.saveout.Click += new System.EventHandler(this.saveout_Click); this.search.TabIndex = 2;
// this.search.Text = "查 询 关 键 词";
// readin this.search.UseVisualStyleBackColor = true;
// this.search.Click += new System.EventHandler(this.search_Click);
this.readin.Location = new System.Drawing.Point(19, 29); //
this.readin.Name = "readin"; // max
this.readin.Size = new System.Drawing.Size(73, 23); //
this.readin.TabIndex = 10; this.max.Location = new System.Drawing.Point(663, 10);
this.readin.Text = "导入关键词"; this.max.Name = "max";
this.readin.UseVisualStyleBackColor = true; this.max.Size = new System.Drawing.Size(83, 21);
this.readin.Click += new System.EventHandler(this.readin_Click); this.max.TabIndex = 3;
// this.max.Text = "20";
// openFile //
// // label1
this.openFile.FileName = "openFileDialog1"; //
// this.label1.AutoSize = true;
// clearresult this.label1.Location = new System.Drawing.Point(592, 13);
// this.label1.Name = "label1";
this.clearresult.Location = new System.Drawing.Point(19, 264); this.label1.Size = new System.Drawing.Size(65, 12);
this.clearresult.Name = "clearresult"; this.label1.TabIndex = 7;
this.clearresult.Size = new System.Drawing.Size(73, 23); this.label1.Text = "查询页数: ";
this.clearresult.TabIndex = 11; //
this.clearresult.Text = "清除结果"; // state
this.clearresult.UseVisualStyleBackColor = true; //
this.clearresult.Click += new System.EventHandler(this.clearresult_Click); this.state.AutoSize = true;
// this.state.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
// clearsearch this.state.ForeColor = System.Drawing.SystemColors.ControlText;
// this.state.Location = new System.Drawing.Point(98, 267);
this.clearsearch.Location = new System.Drawing.Point(100, 29); this.state.Name = "state";
this.clearsearch.Name = "clearsearch"; this.state.Size = new System.Drawing.Size(37, 14);
this.clearsearch.Size = new System.Drawing.Size(73, 23); this.state.TabIndex = 8;
this.clearsearch.TabIndex = 12; this.state.Text = "状态";
this.clearsearch.Text = "清除关键词"; this.state.TextAlign = System.Drawing.ContentAlignment.TopCenter;
this.clearsearch.UseVisualStyleBackColor = true; //
this.clearsearch.Click += new System.EventHandler(this.clearsearch_Click); // saveout
// //
// company this.saveout.Location = new System.Drawing.Point(181, 29);
// this.saveout.Name = "saveout";
this.company.HeaderText = "公司网址"; this.saveout.Size = new System.Drawing.Size(73, 23);
this.company.Name = "company"; this.saveout.TabIndex = 9;
this.company.Width = 150; this.saveout.Text = "导出关键词";
// this.saveout.UseVisualStyleBackColor = true;
// 百度s this.saveout.Click += new System.EventHandler(this.saveout_Click);
// //
dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; // readin
this.s.DefaultCellStyle = dataGridViewCellStyle1; //
this.s.HeaderText = "百度第一页"; this.readin.Location = new System.Drawing.Point(19, 29);
this.s.Name = "百度s"; this.readin.Name = "readin";
this.s.Width = 90; this.readin.Size = new System.Drawing.Size(73, 23);
// this.readin.TabIndex = 10;
// 必应s this.readin.Text = "导入关键词";
// this.readin.UseVisualStyleBackColor = true;
dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; this.readin.Click += new System.EventHandler(this.readin_Click);
this.s.DefaultCellStyle = dataGridViewCellStyle2; //
this.s.HeaderText = "必应第一页"; // openFile
this.s.Name = "必应s"; //
this.s.Width = 90; this.openFile.FileName = "openFileDialog1";
// //
// 搜狗s // clearresult
// //
dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; this.clearresult.Location = new System.Drawing.Point(19, 264);
this.s.DefaultCellStyle = dataGridViewCellStyle3; this.clearresult.Name = "clearresult";
this.s.HeaderText = "搜狗第一页"; this.clearresult.Size = new System.Drawing.Size(73, 23);
this.s.Name = "搜狗s"; this.clearresult.TabIndex = 11;
this.s.Width = 90; this.clearresult.Text = "清除结果";
// this.clearresult.UseVisualStyleBackColor = true;
// keyword this.clearresult.Click += new System.EventHandler(this.clearresult_Click);
// //
this.keyword.HeaderText = "关键词(用英文逗号隔开)"; // clearsearch
this.keyword.Name = "keyword"; //
this.keyword.Width = 350; this.clearsearch.Location = new System.Drawing.Point(100, 29);
// this.clearsearch.Name = "clearsearch";
// 公司名称 this.clearsearch.Size = new System.Drawing.Size(73, 23);
// this.clearsearch.TabIndex = 12;
this..HeaderText = "公司网站"; this.clearsearch.Text = "清除关键词";
this..Name = "公司名称"; this.clearsearch.UseVisualStyleBackColor = true;
this..Width = 150; this.clearsearch.Click += new System.EventHandler(this.clearsearch_Click);
// //
// 关键词 // company
// //
this..HeaderText = "关键词"; this.company.HeaderText = "公司网址";
this..Name = "关键词"; this.company.Name = "company";
this..Width = 200; this.company.Width = 150;
// //
// 百度r // keyword
// //
dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; this.keyword.HeaderText = "关键词(用英文逗号隔开)";
this.r.DefaultCellStyle = dataGridViewCellStyle4; this.keyword.Name = "keyword";
this.r.HeaderText = "百度"; this.keyword.Width = 330;
this.r.Name = "百度r"; //
// // 百度s
// 必应r //
// dataGridViewCellStyle1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
dataGridViewCellStyle5.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; this.s.DefaultCellStyle = dataGridViewCellStyle1;
this.r.DefaultCellStyle = dataGridViewCellStyle5; this.s.HeaderText = "百度第一页";
this.r.HeaderText = "必应"; this.s.Name = "百度s";
this.r.Name = "必应r"; this.s.Width = 90;
// //
// 搜狗r // 必应s
// //
dataGridViewCellStyle6.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; dataGridViewCellStyle2.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
this.r.DefaultCellStyle = dataGridViewCellStyle6; this.s.DefaultCellStyle = dataGridViewCellStyle2;
this.r.HeaderText = "搜狗"; this.s.HeaderText = "必应第一页";
this.r.Name = "搜狗r"; this.s.Name = "必应s";
// this.s.Width = 90;
// 查询时间 //
// // 搜狗s
this..HeaderText = "查询时间"; //
this..Name = "查询时间"; dataGridViewCellStyle3.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
this..Width = 130; this.s.DefaultCellStyle = dataGridViewCellStyle3;
// this.s.HeaderText = "搜狗第一页";
// Frm_Main this.s.Name = "搜狗s";
// this.s.Width = 90;
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None; //
this.ClientSize = new System.Drawing.Size(872, 603); // sum
this.Controls.Add(this.clearsearch); //
this.Controls.Add(this.clearresult); dataGridViewCellStyle4.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
this.Controls.Add(this.readin); this.sum.DefaultCellStyle = dataGridViewCellStyle4;
this.Controls.Add(this.saveout); this.sum.HeaderText = "总计";
this.Controls.Add(this.state); this.sum.Name = "sum";
this.Controls.Add(this.label1); this.sum.Width = 55;
this.Controls.Add(this.max); //
this.Controls.Add(this.search); // Frm_Main
this.Controls.Add(this.ResultView); //
this.Controls.Add(this.SearchView); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.ClientSize = new System.Drawing.Size(872, 603);
this.MaximizeBox = false; this.Controls.Add(this.clearsearch);
this.MinimizeBox = false; this.Controls.Add(this.clearresult);
this.Name = "Frm_Main"; this.Controls.Add(this.readin);
this.Text = "SEO排名查询软件 2015-7-30"; this.Controls.Add(this.saveout);
this.Load += new System.EventHandler(this.Frm_Main_Load); this.Controls.Add(this.state);
((System.ComponentModel.ISupportInitialize)(this.SearchView)).EndInit(); this.Controls.Add(this.label1);
((System.ComponentModel.ISupportInitialize)(this.ResultView)).EndInit(); this.Controls.Add(this.max);
this.ResumeLayout(false); this.Controls.Add(this.search);
this.PerformLayout(); this.Controls.Add(this.ResultView);
this.Controls.Add(this.SearchView);
} this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
#endregion this.MinimizeBox = false;
this.Name = "Frm_Main";
private System.Windows.Forms.DataGridView SearchView; this.Text = "SEO排名查询软件 2015-7-30";
private System.Windows.Forms.DataGridView ResultView; this.Load += new System.EventHandler(this.Frm_Main_Load);
private System.Windows.Forms.Button search; ((System.ComponentModel.ISupportInitialize)(this.SearchView)).EndInit();
private System.Windows.Forms.TextBox max; ((System.ComponentModel.ISupportInitialize)(this.ResultView)).EndInit();
private System.Windows.Forms.Label label1; this.ResumeLayout(false);
private System.Windows.Forms.Label state; this.PerformLayout();
private System.Windows.Forms.Button saveout;
private System.Windows.Forms.Button readin; }
private System.Windows.Forms.OpenFileDialog openFile;
private System.Windows.Forms.SaveFileDialog saveFile; #endregion
private System.Windows.Forms.Button clearresult;
private System.Windows.Forms.Button clearsearch; private System.Windows.Forms.DataGridView SearchView;
private System.Windows.Forms.DataGridViewTextBoxColumn company; private System.Windows.Forms.DataGridView ResultView;
private System.Windows.Forms.DataGridViewTextBoxColumn s; private System.Windows.Forms.Button search;
private System.Windows.Forms.DataGridViewTextBoxColumn s; private System.Windows.Forms.TextBox max;
private System.Windows.Forms.DataGridViewTextBoxColumn s; private System.Windows.Forms.Label label1;
private System.Windows.Forms.DataGridViewTextBoxColumn keyword; private System.Windows.Forms.Label state;
private System.Windows.Forms.DataGridViewTextBoxColumn ; private System.Windows.Forms.Button saveout;
private System.Windows.Forms.DataGridViewTextBoxColumn ; private System.Windows.Forms.Button readin;
private System.Windows.Forms.DataGridViewTextBoxColumn r; private System.Windows.Forms.OpenFileDialog openFile;
private System.Windows.Forms.DataGridViewTextBoxColumn r; private System.Windows.Forms.SaveFileDialog saveFile;
private System.Windows.Forms.DataGridViewTextBoxColumn r; private System.Windows.Forms.Button clearresult;
private System.Windows.Forms.DataGridViewTextBoxColumn ; private System.Windows.Forms.Button clearsearch;
private System.Windows.Forms.DataGridViewTextBoxColumn ;
} private System.Windows.Forms.DataGridViewTextBoxColumn ;
} private System.Windows.Forms.DataGridViewTextBoxColumn r;
private System.Windows.Forms.DataGridViewTextBoxColumn r;
private System.Windows.Forms.DataGridViewTextBoxColumn r;
private System.Windows.Forms.DataGridViewTextBoxColumn ;
private System.Windows.Forms.DataGridViewTextBoxColumn company;
private System.Windows.Forms.DataGridViewTextBoxColumn keyword;
private System.Windows.Forms.DataGridViewTextBoxColumn s;
private System.Windows.Forms.DataGridViewTextBoxColumn s;
private System.Windows.Forms.DataGridViewTextBoxColumn s;
private System.Windows.Forms.DataGridViewTextBoxColumn sum;
}
}

View File

@ -1,212 +1,245 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Data; using System.Data;
using System.Drawing; using System.Drawing;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using AliKeywordSearch; using AliKeywordSearch;
using System.Web; using System.Web;
using System.Reflection; using System.Reflection;
using System.Text.RegularExpressions;
namespace EnAliKeywordSearch
{ namespace EnAliKeywordSearch
public partial class Frm_Main : Form {
{ public partial class Frm_Main : Form
public Frm_Main() {
{ public Frm_Main()
InitializeComponent(); {
} InitializeComponent();
}
enum SearchType
{ enum SearchType
[UrlValue("https://www.baidu.com/s?wd={0}&pn={1}0&ie=utf-8")] {
, [UrlValue("https://www.baidu.com/s?wd={0}&pn={1}0&ie=utf-8")]
[UrlValue("http://cn.bing.com/search?q={0}&first={1}1&FORM=PERE1")] ,
, [UrlValue("http://cn.bing.com/search?q={0}&first={1}1&FORM=PERE1")]
[UrlValue("http://www.sogou.com/web?query={0}&page={1}&ie=utf8")] ,
[UrlValue("http://www.sogou.com/web?query={0}&page={1}&ie=utf8")]
}
//360 Can'tUse url = String.Format("http://www.haosou.com/s?q={0}&pn={1}&client=aff-360daohang&ie=utf-8", HttpUtility.UrlEncode(key), i); }
//BINGurl = String.Format("http://cn.bing.com/search?q={0}&first={1}1&FORM=PERE1", HttpUtility.UrlEncode(key), i - 1); //360 Can'tUse url = String.Format("http://www.haosou.com/s?q={0}&pn={1}&client=aff-360daohang&ie=utf-8", HttpUtility.UrlEncode(key), i);
//搜狗url = String.Format("http://www.sogou.com/web?query={0}&page={1}&ie=utf8", HttpUtility.UrlEncode(key), i); //BINGurl = String.Format("http://cn.bing.com/search?q={0}&first={1}1&FORM=PERE1", HttpUtility.UrlEncode(key), i - 1);
//百度url = String.Format("https://www.baidu.com/s?wd={0}&pn={1}0&ie=utf-8", HttpUtility.UrlEncode(key), i - 1); //搜狗url = String.Format("http://www.sogou.com/web?query={0}&page={1}&ie=utf8", HttpUtility.UrlEncode(key), i);
public class UrlValue : System.Attribute //百度url = String.Format("https://www.baidu.com/s?wd={0}&pn={1}0&ie=utf-8", HttpUtility.UrlEncode(key), i - 1);
{ public class UrlValue : System.Attribute
private string _value; {
private string _value;
public UrlValue(string value)
{ public UrlValue(string value)
_value = value; {
} _value = value;
}
public string Value
{ public string Value
get { return _value; } {
} get { return _value; }
}
}
}
public static class UrlEnum
{ public static class UrlEnum
public static string GetUrl(Enum value) {
{ public static string GetUrl(Enum value)
string output = null; {
Type type = value.GetType(); string output = null;
Type type = value.GetType();
FieldInfo fi = type.GetField(value.ToString());
UrlValue[] attrs = FieldInfo fi = type.GetField(value.ToString());
fi.GetCustomAttributes(typeof(UrlValue), UrlValue[] attrs =
false) as UrlValue[]; fi.GetCustomAttributes(typeof(UrlValue),
if (attrs.Length > 0) false) as UrlValue[];
{ if (attrs.Length > 0)
output = attrs[0].Value; {
} output = attrs[0].Value;
return output; }
} return output;
} }
}
private void search_Click(object sender, EventArgs e)
{ private void search_Click(object sender, EventArgs e)
search.Enabled = false; {
state.ForeColor = Color.Black; search.Enabled = false;
state.ForeColor = Color.Red; state.ForeColor = Color.Black;
HttpHelper httpHelper = new HttpHelper(); state.ForeColor = Color.Red;
foreach (DataGridViewRow item in this.SearchView.Rows) HttpHelper httpHelper = new HttpHelper();
{ foreach (DataGridViewRow item in this.SearchView.Rows)
string cpy = item.Cells["company"].Value == null ? "" : item.Cells["company"].Value.ToString(); {
string keys = item.Cells["keyword"].Value == null ? "" : item.Cells["keyword"].Value.ToString(); string cpy = item.Cells["company"].Value == null ? "" : item.Cells["company"].Value.ToString();
if (cpy != "" && keys != "") string keys = item.Cells["keyword"].Value == null ? "" : item.Cells["keyword"].Value.ToString();
{ if (cpy != "" && keys != "")
foreach (string key in keys.Split(',')) {
{ foreach (string key in keys.Split(','))
if (String.IsNullOrEmpty(key)) {
continue; if (String.IsNullOrEmpty(key))
bool maybe = false; continue;
int maxpage = this.ToInt(max.Text); bool maybe = false;
int index = this.ResultView.Rows.Add(); int maxpage = this.ToInt(max.Text);
this.ResultView.FirstDisplayedScrollingRowIndex = index; int index = this.ResultView.Rows.Add();
this.ResultView.Rows[index].Cells["公司名称"].Value = cpy; this.ResultView.FirstDisplayedScrollingRowIndex = index;
this.ResultView.Rows[index].Cells["关键词"].Value = key; this.ResultView.Rows[index].Cells["公司名称"].Value = cpy;
state.ForeColor = Color.Black; this.ResultView.Rows[index].Cells["关键词"].Value = key;
Application.DoEvents(); state.ForeColor = Color.Black;
string pageinfo = string.Empty; Application.DoEvents();
foreach (SearchType s in Enum.GetValues(typeof(SearchType))) string pageinfo = string.Empty;
{ foreach (SearchType s in Enum.GetValues(typeof(SearchType)))
if (item.Cells[s.ToString() + "s"].Value == null) {
item.Cells[s.ToString() + "s"].Value = 0; if (item.Cells[s.ToString() + "s"].Value == null)
state.Text = "正在 " + s.ToString() + " 查询 " + cpy + " 的关键词 " + key; item.Cells[s.ToString() + "s"].Value = 0;
for (int i = 1; i <= maxpage; i++) state.Text = "正在 " + s.ToString() + " 查询 " + cpy + " 的关键词 " + key;
{ for (int i = 1; i <= maxpage; i++)
string url = String.Format(UrlEnum.GetUrl(s), HttpUtility.UrlEncode(key), (s.ToString() == "搜狗" ? i : i - 1)); {
this.ResultView.Rows[index].Cells[s.ToString() + "r"].Value = "查询第" + i + "页..."; string url = String.Format(UrlEnum.GetUrl(s), HttpUtility.UrlEncode(key), (s.ToString() == "搜狗" ? i : i - 1));//s搜狗的页面值不同
Application.DoEvents(); this.ResultView.Rows[index].Cells[s.ToString() + "r"].Value = "查询第" + i + "页...";
string htmldoc = httpHelper.Get(url); Application.DoEvents();
if (string.IsNullOrEmpty(htmldoc)) string htmldoc = httpHelper.Get(url);
{ if (string.IsNullOrEmpty(htmldoc))
state.ForeColor = Color.Red; {
state.Text = "关键词 " + key + " 在 " + s.ToString() + " 第 " + i + " 页 网页抓取失败 错误:" + HttpHelper.ErrMsg; state.ForeColor = Color.Red;
maybe = true; state.Text = "关键词 " + key + " 在 " + s.ToString() + " 第 " + i + " 页 网页抓取失败 错误:" + HttpHelper.ErrMsg;
continue; maybe = true;
} continue;
string encodecpy = HttpUtility.HtmlEncode(cpy); }
if (htmldoc.Contains(encodecpy)) if (htmldoc == "0")
{ {
pageinfo = "第" + i + "页"; this.ResultView.Rows[index].Cells[s.ToString() + "r"].Value = "已被屏蔽";
if (i == 1) this.ResultView.Rows[index].Cells[s.ToString() + "r"].Style.ForeColor = Color.Red;
item.Cells[s.ToString() + "s"].Value = int.Parse(item.Cells[s.ToString() + "s"].Value.ToString()) + 1; break;
this.ResultView.Rows[index].Cells[s.ToString() + "r"].Style.ForeColor = (i == 1 ? Color.Magenta : Color.Black); }
break; string cpyencode = HttpUtility.HtmlEncode(cpy);
} int findindex = htmldoc.IndexOf(cpyencode);
if (i == maxpage) if (findindex > 0)
{ {
pageinfo = maxpage + "页以后"; string check = htmldoc.Substring(findindex, 60);
break; while (findindex > 0)
} {
Application.DoEvents(); check = htmldoc.Substring(findindex, 60);
} Console.WriteLine(s.ToString() + " " + i + " " + check);
this.ResultView.Rows[index].Cells[s.ToString() + "r"].Value = pageinfo + (maybe ? "(可能不准确)" : ""); if (!check.Contains("\\/"))
this.ResultView.Rows[index].Cells[s.ToString() + "r"].Style.ForeColor = maybe ? Color.Red : this.ResultView.Rows[index].Cells[s.ToString() + "r"].Style.ForeColor; {
this.ResultView.Rows[index].Cells["查询时间"].Value = DateTime.Now.ToString(); break;
Application.DoEvents(); }
} findindex = htmldoc.IndexOf(cpyencode, findindex + 1);
} }
} if ((check.Contains("span") || check.Contains("><") || check.Contains(".html") || check.Contains("href") || check.Contains("%")))
{
Application.DoEvents(); pageinfo = "第" + i + "页";
} if (i == 1)
state.ForeColor = Color.Green; item.Cells[s.ToString() + "s"].Value = int.Parse(item.Cells[s.ToString() + "s"].Value.ToString()) + 1;
state.Text = "所有关键词查询完成!"; this.ResultView.Rows[index].Cells[s.ToString() + "r"].Style.ForeColor = (i == 1 ? Color.Magenta : Color.Black);
search.Enabled = true; break;
} }
}
private int ToInt(String str) if (i == maxpage)
{ {
int s = 0; pageinfo = maxpage + "页以后";
int.TryParse(str, out s); break;
return s; }
} Application.DoEvents();
}
private void Frm_Main_Load(object sender, EventArgs e) this.ResultView.Rows[index].Cells[s.ToString() + "r"].Value = pageinfo + (maybe ? "(可能不准确)" : "");
{ this.ResultView.Rows[index].Cells[s.ToString() + "r"].Style.ForeColor = maybe ? Color.Red : this.ResultView.Rows[index].Cells[s.ToString() + "r"].Style.ForeColor;
this.ResultView.Rows[index].Cells["查询时间"].Value = DateTime.Now.ToString();
} Application.DoEvents();
#region 窗体事件 }
private void saveout_Click(object sender, EventArgs e) Console.WriteLine("==================" + key + "==================");
{ }
SaveFileDialog cpm = new SaveFileDialog(); }
cpm.Filter = "查排名数据|*.cpm"; if (item.Cells["keyword"].Value != null)
if (cpm.ShowDialog() == DialogResult.OK) {
{ if (item.Cells["sum"].Value == null)
List<string> data = new List<string>(); item.Cells["sum"].Value = 0;
foreach (DataGridViewRow item in this.SearchView.Rows) foreach (SearchType s in Enum.GetValues(typeof(SearchType)))
{ {
string cpy = item.Cells["company"].Value == null ? "" : item.Cells["company"].Value.ToString(); int all = int.Parse(item.Cells["sum"].Value.ToString());
string keys = item.Cells["keyword"].Value == null ? "" : item.Cells["keyword"].Value.ToString(); int add = int.Parse(item.Cells[s.ToString() + "s"].Value.ToString());
if (cpy != "" && keys != "") item.Cells["sum"].Value = all + add;
data.Add(cpy + "|" + keys); }
} }
Config cfg = new Config(cpm.FileName); Application.DoEvents();
cfg.WriteListToTextFile(data); }
} state.ForeColor = Color.Green;
} state.Text = "所有关键词查询完成!";
search.Enabled = true;
private void readin_Click(object sender, EventArgs e) }
{
OpenFileDialog cpm = new OpenFileDialog(); private int ToInt(String str)
cpm.Filter = "查排名数据|*.cpm"; {
if (cpm.ShowDialog() == DialogResult.OK) int s = 0;
{ int.TryParse(str, out s);
Config cfg = new Config(cpm.FileName); return s;
List<string> data = cfg.ReadTextFileToList(); }
string cpy = string.Empty;
string keys = string.Empty; private void Frm_Main_Load(object sender, EventArgs e)
foreach (string item in data) {
{
string[] str = item.Split('|'); }
if (str.Length == 2) #region 窗体事件
{ private void saveout_Click(object sender, EventArgs e)
cpy = str[0]; {
keys = str[1]; SaveFileDialog cpm = new SaveFileDialog();
int index = this.SearchView.Rows.Add(); cpm.Filter = "查排名数据|*.cpm";
this.SearchView.Rows[index].Cells["company"].Value = cpy; if (cpm.ShowDialog() == DialogResult.OK)
this.SearchView.Rows[index].Cells["keyword"].Value = keys; {
} List<string> data = new List<string>();
} foreach (DataGridViewRow item in this.SearchView.Rows)
} {
} string cpy = item.Cells["company"].Value == null ? "" : item.Cells["company"].Value.ToString();
string keys = item.Cells["keyword"].Value == null ? "" : item.Cells["keyword"].Value.ToString();
private void clearresult_Click(object sender, EventArgs e) if (cpy != "" && keys != "")
{ data.Add(cpy + "|" + keys);
ResultView.Rows.Clear(); }
} Config cfg = new Config(cpm.FileName);
cfg.WriteListToTextFile(data);
private void clearsearch_Click(object sender, EventArgs e) }
{ }
SearchView.Rows.Clear();
} private void readin_Click(object sender, EventArgs e)
#endregion {
} OpenFileDialog cpm = new OpenFileDialog();
} cpm.Filter = "查排名数据|*.cpm";
if (cpm.ShowDialog() == DialogResult.OK)
{
Config cfg = new Config(cpm.FileName);
List<string> data = cfg.ReadTextFileToList();
string cpy = string.Empty;
string keys = string.Empty;
foreach (string item in data)
{
string[] str = item.Split('|');
if (str.Length == 2)
{
cpy = str[0];
keys = str[1];
int index = this.SearchView.Rows.Add();
this.SearchView.Rows[index].Cells["company"].Value = cpy;
this.SearchView.Rows[index].Cells["keyword"].Value = keys;
}
}
}
}
private void clearresult_Click(object sender, EventArgs e)
{
ResultView.Rows.Clear();
}
private void clearsearch_Click(object sender, EventArgs e)
{
SearchView.Rows.Clear();
}
#endregion
}
}

View File

@ -120,6 +120,9 @@
<metadata name="company.UserAddedColumn" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="company.UserAddedColumn" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value> <value>True</value>
</metadata> </metadata>
<metadata name="keyword.UserAddedColumn" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="百度s.UserAddedColumn" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="百度s.UserAddedColumn" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value> <value>True</value>
</metadata> </metadata>
@ -129,7 +132,7 @@
<metadata name="搜狗s.UserAddedColumn" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="搜狗s.UserAddedColumn" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value> <value>True</value>
</metadata> </metadata>
<metadata name="keyword.UserAddedColumn" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="sum.UserAddedColumn" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value> <value>True</value>
</metadata> </metadata>
<metadata name="公司名称.UserAddedColumn" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="公司名称.UserAddedColumn" type="System.Boolean, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">

View File

@ -1,364 +1,367 @@
using System; using System;
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
using System.IO.Compression; using System.IO.Compression;
using System.Net; using System.Net;
using System.Net.Security; using System.Net.Security;
using System.Net.Sockets; using System.Net.Sockets;
using System.Security.Authentication; using System.Security.Authentication;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
namespace EnAliKeywordSearch namespace EnAliKeywordSearch
{ {
public class HttpArgs public class HttpArgs
{ {
public enum HttpMethod public enum HttpMethod
{ {
GET, GET,
POST POST
} }
public string Url { get; set; } public string Url { get; set; }
public string Host { get; set; } public string Host { get; set; }
public int Port { get; set; } public int Port { get; set; }
public string Accept { get; set; } public string Accept { get; set; }
public string Referer { get; set; } public string Referer { get; set; }
public string Cookie { get; set; } public string Cookie { get; set; }
public string Data { get; set; } public string Data { get; set; }
public string UA { get; set; } public string UA { get; set; }
public HttpMethod Method { get; set; } public HttpMethod Method { get; set; }
} }
public class HttpHelper public class HttpHelper
{ {
public static int State = 0; public static int State = 0;
public static string ErrMsg = string.Empty; public static string ErrMsg = string.Empty;
/// <summary> /// <summary>
/// 提交方法 /// 提交方法
/// </summary> /// </summary>
#region HttpWebRequest & HttpWebResponse #region HttpWebRequest & HttpWebResponse
/// <summary> /// <summary>
/// Get方法 /// Get方法
/// </summary> /// </summary>
/// <param name="geturl">请求地址</param> /// <param name="geturl">请求地址</param>
/// <param name="cookieser">Cookies存储器</param> /// <param name="cookieser">Cookies存储器</param>
/// <returns>请求返回的Stream</returns> /// <returns>请求返回的Stream</returns>
public string Get(string url) public string Get(string url)
{ {
HttpArgs args = ParseURL(url); HttpArgs args = ParseURL(url);
args.Method = HttpArgs.HttpMethod.GET; args.Method = HttpArgs.HttpMethod.GET;
string strhtml = InternalSocketHttp(args); string strhtml = InternalSocketHttp(args);
return strhtml; return strhtml;
} }
/// <summary> /// <summary>
/// Post方法 /// Post方法
/// </summary> /// </summary>
/// <param name="posturl">请求地址</param> /// <param name="posturl">请求地址</param>
/// <param name="bytes">Post数据</param> /// <param name="bytes">Post数据</param>
/// <param name="cookieser">Cllkies存储器</param> /// <param name="cookieser">Cllkies存储器</param>
/// <returns>请求返回的流</returns> /// <returns>请求返回的流</returns>
public string Post(string url, public string Post(string url,
byte[] bytes, byte[] bytes,
CookieContainer cookies, CookieContainer cookies,
Encoding encoding) Encoding encoding)
{ {
return null; return null;
} }
/// <summary> /// <summary>
/// 根据Url得到host /// 根据Url得到host
/// </summary> /// </summary>
/// <param name="strUrl">url字符串</param> /// <param name="strUrl">url字符串</param>
/// <returns>host字符串</returns> /// <returns>host字符串</returns>
private HttpArgs ParseURL(string strUrl) private HttpArgs ParseURL(string strUrl)
{ {
HttpArgs args = new HttpArgs(); HttpArgs args = new HttpArgs();
args.Host = ""; args.Host = "";
args.Port = 80; args.Port = 80;
args.Referer = ""; args.Referer = "";
args.Cookie = ""; args.Cookie = "";
args.Url = ""; args.Url = "";
args.Accept = "text/html";//,application/xhtml+xml,application/xml,application/json;"; args.Accept = "text/html";//,application/xhtml+xml,application/xml,application/json;"
args.UA = "Mozilla/5.0+(Compatible;+Baiduspider/2.0;++http://www.baidu.com/search/spider.html)";
//args.UA = "Mozilla/5.0+(Compatible;+Baiduspider/2.0;++http://www.baidu.com/search/spider.html)";
//http://www.alibaba.com/products/Egg_Laying_Block_Machine/1.html //args.UA = @"Mozilla/5.0 (Linux;u;Android 4.2.2;zh-cn;) AppleWebKit/534.46 (KHTML,like Gecko) Version/5.1 Mobile Safari/10600.6.3 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)";
int iIndex = strUrl.IndexOf(@"//"); args.UA = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 UBrowser/5.2.2603.31 Safari/537.36";
if (iIndex <= 0)
return null; //http://www.alibaba.com/products/Egg_Laying_Block_Machine/1.html
//www.alibaba.com:80/products/Egg_Laying_Block_Machine/1.html int iIndex = strUrl.IndexOf(@"//");
string nohttpurl = strUrl.Substring(iIndex + 2); if (iIndex <= 0)
string address = nohttpurl; return null;
iIndex = nohttpurl.IndexOf(@"/"); //www.alibaba.com:80/products/Egg_Laying_Block_Machine/1.html
if (iIndex > 0) string nohttpurl = strUrl.Substring(iIndex + 2);
{ string address = nohttpurl;
//www.alibaba.com:80 iIndex = nohttpurl.IndexOf(@"/");
address = nohttpurl.Substring(0, iIndex); if (iIndex > 0)
args.Url = nohttpurl.Substring(iIndex); {
} //www.alibaba.com:80
iIndex = nohttpurl.IndexOf(@":"); address = nohttpurl.Substring(0, iIndex);
if (iIndex > 0) args.Url = nohttpurl.Substring(iIndex);
{ }
string[] tempargs = nohttpurl.Trim().Split(char.Parse(":")); iIndex = nohttpurl.IndexOf(@":");
args.Host = tempargs[0]; if (iIndex > 0)
args.Port = int.Parse(tempargs[1]); {
} string[] tempargs = nohttpurl.Trim().Split(char.Parse(":"));
else args.Host = tempargs[0];
{ args.Port = int.Parse(tempargs[1]);
//www.alibaba.com:80 }
args.Host = address; else
args.Port = 80; {
} //www.alibaba.com:80
return args; args.Host = address;
} args.Port = 80;
#endregion }
return args;
#region Socket }
#endregion
string InternalSocketHttp(HttpArgs args)
{ #region Socket
using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{ string InternalSocketHttp(HttpArgs args)
try {
{ using (Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000); {
socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000); try
socket.Connect(args.Host, args.Port); {
if (socket.Connected) socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 1000);
{ socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 5000);
byte[] buff = ParseHttpArgs(args); socket.Connect(args.Host, args.Port);
if (socket.Send(buff) > 0) if (socket.Connected)
{ {
List<byte> responseBytes = new List<byte>(); byte[] buff = ParseHttpArgs(args);
byte[] buffer = new byte[1024]; if (socket.Send(buff) > 0)
int iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None); {
while (iNumber > 0)//使用了Connection: Close 所以判断长度为0 时停止接受 List<byte> responseBytes = new List<byte>();
{ byte[] buffer = new byte[1024];
responseBytes.AddRange(new List<byte>(buffer));//添加数据到List int iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None);
iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None);//继续接收数据 while (iNumber > 0)//使用了Connection: Close 所以判断长度为0 时停止接受
} {
return ParseResponse(responseBytes.ToArray()/*转换List为数组*/, args); responseBytes.AddRange(new List<byte>(buffer));//添加数据到List
} iNumber = socket.Receive(buffer, buffer.Length, SocketFlags.None);//继续接收数据
} }
} return ParseResponse(responseBytes.ToArray()/*转换List为数组*/, args);
catch (Exception e) }
{ }
ErrMsg = e.Message; }
} catch (Exception e)
return string.Empty; {
} ErrMsg = e.Message;
} }
return string.Empty;
private string ParseResponse(byte[] responseBytes, HttpArgs args) }
{ }
string responseStr = Encoding.UTF8.GetString(responseBytes);
int splitindex = responseStr.IndexOf("\r\n\r\n"); private string ParseResponse(byte[] responseBytes, HttpArgs args)
if (splitindex > 0) {
{ string responseStr = Encoding.UTF8.GetString(responseBytes);
string responseHeader = responseStr.Substring(0, splitindex); int splitindex = responseStr.IndexOf("\r\n\r\n");
string responseBody = responseStr.Substring(splitindex + 4); if (splitindex > 0)
{
if (responseHeader.StartsWith("HTTP/1.1 400 Bad Request")) string responseHeader = responseStr.Substring(0, splitindex);
{ string responseBody = responseStr.Substring(splitindex + 4);
State = 400;
return string.Empty; if (responseHeader.StartsWith("HTTP/1.1 400 Bad Request"))
} {
else if (responseHeader.StartsWith("HTTP/1.1 404")) State = 400;
{ return string.Empty;
State = 404; }
return string.Empty; else if (responseHeader.StartsWith("HTTP/1.1 404"))
} {
else if (responseHeader.StartsWith("HTTP/1.1 302") || responseHeader.StartsWith("HTTP/1.1 301")) State = 404;
{ return string.Empty;
State = 302; }
int start = responseHeader.ToUpper().IndexOf("LOCATION"); else if (responseHeader.StartsWith("HTTP/1.1 302") || responseHeader.StartsWith("HTTP/1.1 301"))
if (start > 0) {
{ State = 302;
string temp = responseHeader.Substring(start, responseHeader.Length - start); int start = responseHeader.ToUpper().IndexOf("LOCATION");
string[] sArry = Regex.Split(temp, "\r\n"); if (start > 0)
args.Url = sArry[0].Remove(0, 10); {
if (args.Url == "") string temp = responseHeader.Substring(start, responseHeader.Length - start);
return string.Empty; string[] sArry = Regex.Split(temp, "\r\n");
return InternalSocketHttp(args); //注意302协议需要重定向 args.Url = sArry[0].Remove(0, 10);
} if (args.Url == "")
} return string.Empty;
else if (responseHeader.StartsWith("HTTP/1.1 200")) //读取内容 return InternalSocketHttp(args); //注意302协议需要重定向
{ }
State = 200; }
DecompressWebPage(ref responseBytes, responseHeader); else if (responseHeader.StartsWith("HTTP/1.1 200")) //读取内容
//转码 {
responseBody = DecodeWebStringByHttpHeader(responseBytes, responseHeader); State = 200;
responseBody = DecodeWebStringByHtmlPageInfo(responseBytes, responseBody); DecompressWebPage(ref responseBytes, responseHeader);
} //转码
splitindex = responseBody.IndexOf("\r\n\r\n"); responseBody = DecodeWebStringByHttpHeader(responseBytes, responseHeader);
if (splitindex > 0) responseBody = DecodeWebStringByHtmlPageInfo(responseBytes, responseBody);
responseBody = responseBody.Substring(splitindex + 4); }
else splitindex = responseBody.IndexOf("\r\n\r\n");
responseBody = string.Empty; if (splitindex > 0)
return responseBody; responseBody = responseBody.Substring(splitindex + 4);
} else
return string.Empty; responseBody = string.Empty;
} return responseBody;
#endregion }
return string.Empty;
#region Helper }
#endregion
/// <summary>
/// 解压网页 #region Helper
/// </summary>
/// <param name="responseBytes">网页字节数组含http头</param> /// <summary>
/// <param name="iTotalCount">数组长度</param> /// 解压网页
/// <param name="strHeader">Http头字符串</param> /// </summary>
/// <param name="iStart">网页正文开始位置</param> /// <param name="responseBytes">网页字节数组含http头</param>
private void DecompressWebPage(ref byte[] responseBytes, string strHeader) /// <param name="iTotalCount">数组长度</param>
{ /// <param name="strHeader">Http头字符串</param>
Regex regZip = new Regex(@"Content-Encoding:\s+gzip[^\n]*\r\n", RegexOptions.IgnoreCase); /// <param name="iStart">网页正文开始位置</param>
private void DecompressWebPage(ref byte[] responseBytes, string strHeader)
if (regZip.IsMatch(strHeader)) {
{ Regex regZip = new Regex(@"Content-Encoding:\s+gzip[^\n]*\r\n", RegexOptions.IgnoreCase);
responseBytes = Decompress(responseBytes);
} if (regZip.IsMatch(strHeader))
} {
responseBytes = Decompress(responseBytes);
/// <summary> }
/// 解压gzip网页 }
/// </summary>
/// <param name="szSource">压缩过的字符串字节数组</param> /// <summary>
/// <returns>解压后的字节数组</returns> /// 解压gzip网页
private byte[] Decompress(byte[] szSource) /// </summary>
{ /// <param name="szSource">压缩过的字符串字节数组</param>
MemoryStream msSource = new MemoryStream(szSource); /// <returns>解压后的字节数组</returns>
//DeflateStream 也可以这儿 private byte[] Decompress(byte[] szSource)
GZipStream stream = new GZipStream(msSource, CompressionMode.Decompress); {
byte[] szTotal = new byte[40 * 1024]; MemoryStream msSource = new MemoryStream(szSource);
long lTotal = 0; //DeflateStream 也可以这儿
byte[] buffer = new byte[8]; GZipStream stream = new GZipStream(msSource, CompressionMode.Decompress);
int iCount = 0; byte[] szTotal = new byte[40 * 1024];
do long lTotal = 0;
{ byte[] buffer = new byte[8];
iCount = stream.Read(buffer, 0, 8); int iCount = 0;
if (szTotal.Length <= lTotal + iCount) //放大数组 do
{ {
byte[] temp = new byte[szTotal.Length * 10]; iCount = stream.Read(buffer, 0, 8);
szTotal.CopyTo(temp, 0); if (szTotal.Length <= lTotal + iCount) //放大数组
szTotal = temp; {
} byte[] temp = new byte[szTotal.Length * 10];
buffer.CopyTo(szTotal, lTotal); szTotal.CopyTo(temp, 0);
lTotal += iCount; szTotal = temp;
} while (iCount != 0); }
byte[] szDest = new byte[lTotal]; buffer.CopyTo(szTotal, lTotal);
Array.Copy(szTotal, 0, szDest, 0, lTotal); lTotal += iCount;
return szDest; } while (iCount != 0);
} byte[] szDest = new byte[lTotal];
Array.Copy(szTotal, 0, szDest, 0, lTotal);
/// <summary> return szDest;
/// 根据Http头标记里面的字符编码解析字符串 }
/// </summary>
/// <param name="responseBytes">网页内容字节数组(除http头以外的内容)</param> /// <summary>
/// <param name="iTotalCount">网页内容字节数组长度</param> /// 根据Http头标记里面的字符编码解析字符串
/// <param name="strHeader">http头的字符串</param> /// </summary>
/// <returns>转好的字符串</returns> /// <param name="responseBytes">网页内容字节数组(除http头以外的内容)</param>
private string DecodeWebStringByHttpHeader(byte[] responseBytes, string strHeader) /// <param name="iTotalCount">网页内容字节数组长度</param>
{ /// <param name="strHeader">http头的字符串</param>
string strResponse = ""; /// <returns>转好的字符串</returns>
if (strHeader.Contains("charset=GBK") || strHeader.Contains("charset=gb2312")) private string DecodeWebStringByHttpHeader(byte[] responseBytes, string strHeader)
{ {
strResponse = Encoding.GetEncoding("GBK").GetString(responseBytes); string strResponse = "";
} if (strHeader.Contains("charset=GBK") || strHeader.Contains("charset=gb2312"))
else {
strResponse = Encoding.UTF8.GetString(responseBytes); strResponse = Encoding.GetEncoding("GBK").GetString(responseBytes);
return strResponse; }
} else
strResponse = Encoding.UTF8.GetString(responseBytes);
/// <summary> return strResponse;
/// 根据网页meta标记里面的字符编码解析字符串 }
/// </summary>
/// <param name="responseBytes">网页内容字节数组(除http头以外的内容)</param> /// <summary>
/// <param name="iTotalCount">网页内容字节数组长度</param> /// 根据网页meta标记里面的字符编码解析字符串
/// <param name="strResponse">网页内容字符串, 可能已经根据其它转码要求转换过的字符串</param> /// </summary>
/// <returns>转好的字符串</returns> /// <param name="responseBytes">网页内容字节数组(除http头以外的内容)</param>
private string DecodeWebStringByHtmlPageInfo(byte[] responseBytes, string strResponse) /// <param name="iTotalCount">网页内容字节数组长度</param>
{ /// <param name="strResponse">网页内容字符串, 可能已经根据其它转码要求转换过的字符串</param>
Regex regGB2312 = new Regex(@"<meta[^>]+Content-Type[^>]+gb2312[^>]*>", RegexOptions.IgnoreCase); /// <returns>转好的字符串</returns>
Regex regGBK = new Regex(@"<meta[^>]+Content-Type[^>]+gbk[^>]*>", RegexOptions.IgnoreCase); private string DecodeWebStringByHtmlPageInfo(byte[] responseBytes, string strResponse)
Regex regBig5 = new Regex(@"<meta[^>]+Content-Type[^>]+Big5[^>]*>", RegexOptions.IgnoreCase); {
if (regGB2312.IsMatch(strResponse) || regGBK.IsMatch(strResponse)) Regex regGB2312 = new Regex(@"<meta[^>]+Content-Type[^>]+gb2312[^>]*>", RegexOptions.IgnoreCase);
strResponse = Encoding.GetEncoding("GBK").GetString(responseBytes); Regex regGBK = new Regex(@"<meta[^>]+Content-Type[^>]+gbk[^>]*>", RegexOptions.IgnoreCase);
if (regBig5.IsMatch(strResponse)) Regex regBig5 = new Regex(@"<meta[^>]+Content-Type[^>]+Big5[^>]*>", RegexOptions.IgnoreCase);
strResponse = Encoding.GetEncoding("Big5").GetString(responseBytes); if (regGB2312.IsMatch(strResponse) || regGBK.IsMatch(strResponse))
return strResponse; strResponse = Encoding.GetEncoding("GBK").GetString(responseBytes);
} if (regBig5.IsMatch(strResponse))
strResponse = Encoding.GetEncoding("Big5").GetString(responseBytes);
private byte[] ParseHttpArgs(HttpArgs args) return strResponse;
{ }
StringBuilder bulider = new StringBuilder();
if (args.Method == HttpArgs.HttpMethod.POST) private byte[] ParseHttpArgs(HttpArgs args)
{ {
bulider.AppendLine(string.Format("POST {0} HTTP/1.1", args.Url)); StringBuilder bulider = new StringBuilder();
bulider.AppendLine("Content-Type: application/x-www-form-urlencoded"); if (args.Method == HttpArgs.HttpMethod.POST)
} {
else bulider.AppendLine(string.Format("POST {0} HTTP/1.1", args.Url));
{ bulider.AppendLine("Content-Type: application/x-www-form-urlencoded");
bulider.AppendLine(string.Format("GET {0} HTTP/1.1", args.Url)); }
} else
{
bulider.AppendLine(string.Format("Host: {0}:{1}", args.Host, args.Port)); bulider.AppendLine(string.Format("GET {0} HTTP/1.1", args.Url));
}
bulider.AppendLine("User-Agent: " + args.UA);
//"User-Agent: Mozilla/5.0+(Compatible;+Baiduspider/2.0;++http://www.baidu.com/search/spider.html)";Mozilla/5.0 (Windows NT 6.1; IE 9.0) bulider.AppendLine(string.Format("Host: {0}:{1}", args.Host, args.Port));
if (!string.IsNullOrEmpty(args.Referer))
bulider.AppendLine(string.Format("Referer: {0}", args.Referer)); bulider.AppendLine("User-Agent: " + args.UA);
//"User-Agent: Mozilla/5.0+(Compatible;+Baiduspider/2.0;++http://www.baidu.com/search/spider.html)";Mozilla/5.0 (Windows NT 6.1; IE 9.0)
//bulider.AppendLine("Connection: close"); if (!string.IsNullOrEmpty(args.Referer))
bulider.AppendLine(string.Format("Referer: {0}", args.Referer));
bulider.AppendLine("Connection: Close");
//bulider.AppendLine("Connection: close");
if (!string.IsNullOrEmpty(args.Accept))
bulider.AppendLine(string.Format("Accept: {0}", args.Accept)); bulider.AppendLine("Connection: Close");
if (!string.IsNullOrEmpty(args.Cookie)) if (!string.IsNullOrEmpty(args.Accept))
bulider.AppendLine(string.Format("Cookie: {0}", args.Cookie)); bulider.AppendLine(string.Format("Accept: {0}", args.Accept));
if (args.Method == HttpArgs.HttpMethod.POST) if (!string.IsNullOrEmpty(args.Cookie))
{ bulider.AppendLine(string.Format("Cookie: {0}", args.Cookie));
bulider.AppendLine(string.Format("Content-Length: {0}\r\n", Encoding.Default.GetBytes(args.Data).Length));
bulider.Append(args.Data); if (args.Method == HttpArgs.HttpMethod.POST)
} {
else bulider.AppendLine(string.Format("Content-Length: {0}\r\n", Encoding.Default.GetBytes(args.Data).Length));
{ bulider.Append(args.Data);
bulider.Append("\r\n"); }
} else
{
string header = bulider.ToString(); bulider.Append("\r\n");
return Encoding.Default.GetBytes(header); }
}
#endregion string header = bulider.ToString();
return Encoding.Default.GetBytes(header);
} }
#endregion
public class MilliTimer
{ }
private static double times { get; set; }
public static void start() public class MilliTimer
{ {
times = getTotalMilliseconds(); private static double times { get; set; }
} public static void start()
{
public static double getTimes() times = getTotalMilliseconds();
{ }
return getTotalMilliseconds() - times;
} public static double getTimes()
{
return getTotalMilliseconds() - times;
public static double getTotalMilliseconds() }
{
return DateTime.Now.Subtract(DateTime.Parse("1970-1-1")).TotalMilliseconds;
} public static double getTotalMilliseconds()
} {
return DateTime.Now.Subtract(DateTime.Parse("1970-1-1")).TotalMilliseconds;
}
}
} }