public HttpInfo CreateHttp() { HttpInfo info = new HttpInfo(); #region 请求头配置 info = new HttpInfo(textBox1.Text); if (!String.IsNullOrEmpty(richTextBox4.Text)) { info.PostData = richTextBox4.Text; } info.UseSystemProxy = checkBox10.Checked; info.Host = textBox17.Text; info.IgnoreWebException = checkBox9.Checked; info.User_Agent = textBox3.Text; info.Referer = textBox4.Text; info.ContentType = textBox5.Text; info.Accept = textBox6.Text; info.AcceptEncoding = textBox7.Text; if (!String.IsNullOrEmpty(textBox19.Text)) { var arr = textBox19.Text.Split('|'); if (arr.Length > 0) { info.Ip = arr[0]; } if (arr.Length > 1) { info.Proxy_UserName = arr[1]; } if (arr.Length > 2) { info.Proxy_PassWord = arr[2]; } } info.CheckUrl = checkBox7.Checked; if (checkBox8.Checked) { info.Expect100Continue = true; } String httpversion = Convert.ToString(comboBox1.SelectedItem); switch (httpversion) { case "1.0": info.ProtocolVersion = ProtocolVersionEnum.V10; break; case "1.1": info.ProtocolVersion = ProtocolVersionEnum.V11; break; } if (!String.IsNullOrEmpty(textBox8.Text)) { info.Encoding = Encoding.GetEncoding(textBox8.Text); } if (!String.IsNullOrEmpty(textBox11.Text)) { info.Cookie = new CookieString(textBox11.Text, true); } else { info.CC = new CookieContainer(); } info.AllowAutoRedirect = checkBox1.Checked; info.KeepLive = checkBox2.Checked; if (checkBox3.Checked) { info.Header.Add("X-Requested-With", "XMLHttpRequest"); } foreach (var item in HeadDic) { info.Header.Add(item.Key, item.Value); } #endregion return(info); }
/// <summary> /// 下载文件按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void barButtonItem14_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { GC.Collect(); #region 请求头配置 info = CreateHttp(); #endregion xtraTabPage3.Controls.Clear(); String checkurl = ".+\\..+"; if (String.IsNullOrEmpty(textEdit1.Text)) { Form4 form4 = new Form4("错误:请求地址不合法"); form4.Text = "错误"; form4.ShowDialog(); barStaticItem1.Caption = "当前状态:取消文件下载"; return; } if (!RegexMethod.CheckRegex(checkurl, textEdit1.Text)) { Form5 form5 = new Form5("警告:未检测到常规格式的下载地址,是否继续"); form5.Text = "警告"; form5.ShowDialog(); if (form5.DialogResult == DialogResult.Cancel) { barStaticItem1.Caption = "当前状态:取消文件下载"; return; } } SaveFileDialog sf = new SaveFileDialog(); var arr = textEdit1.Text.Split('/'); String filename = arr[arr.Length - 1]; String type = "*"; String name = filename; if (filename.Contains(".")) { type = filename.Split('.')[1]; name = filename.Split('.')[0]; } else { Form4 form4 = new Form4(""); form4.Text = "自定义文件后缀"; form4.ShowDialog(); type = form4.filter; } String filter = String.Format("{0}(*.{0})|*.{0}|所有文件(*.*)|*.*", type, type, type); sf.Filter = filter;//可以保存的格式 sf.FileName = arr[arr.Length - 1]; if (sf.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (HttpMethod.DownLoadFile_ABPath(info, sf.FileName)) { Form4 form4 = new Form4("成功:下载成功"); form4.Text = "成功"; form4.ShowDialog(); } else { Form4 form4 = new Form4("失败:下载失败"); form4.Text = "失败"; form4.ShowDialog(); } } barStaticItem1.Caption = "当前状态:完毕"; }
public HttpAB(HttpInfo aBDownInfo) : base(aBDownInfo) { }
/// <summary> /// 下载图片按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void barButtonItem12_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { GC.Collect(); xtraTabPage3.Controls.Clear(); String checkurl = ".+\\..+"; if (String.IsNullOrEmpty(textEdit1.Text)) { Form4 form4 = new Form4("错误:请求地址不合法"); form4.Text = "错误"; form4.ShowDialog(); barStaticItem1.Caption = "当前状态:取消图片请求"; return; } if (!RegexMethod.CheckRegex(checkurl, textEdit1.Text)) { Form5 form5 = new Form5("警告:未检测到常规格式的下载地址,是否继续"); form5.Text = "警告"; form5.ShowDialog(); if (form5.DialogResult == DialogResult.Cancel) { barStaticItem1.Caption = "当前状态:取消图片下载"; return; } } barStaticItem1.Caption = "下载图片:" + Path.GetFileName(textEdit1.Text); xtraTabPage1.Controls.Clear(); xtraTabPage2.Controls.Clear(); html = String.Empty; Jhtml = String.Empty; #region 请求头配置 info = CreateHttp(); #endregion try { Image img = HttpMethod.DownPic(info); if (img == null) { Form4 form4 = new Form4("失败:下载失败"); form4.Text = "失败"; form4.ShowDialog(); return; } PictureEdit pictureEdit = new PictureEdit(); pictureEdit.Dock = DockStyle.Fill; pictureEdit.Image = img; xtraTabPage3.Controls.Add(pictureEdit); } catch { Form4 form4 = new Form4("程序错误:请联系程序员!"); form4.Text = "错误"; form4.ShowDialog(); } barStaticItem1.Caption = "下载图片:" + Path.GetFileName(textEdit1.Text) + "完毕"; tabPane1.SelectedPage = tabNavigationPage2; }
/// <summary> /// 请求按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) { GC.Collect(); #region 请求前检查 if (String.IsNullOrEmpty(textEdit1.Text)) { Form4 form4 = new Form4("错误:请求地址不合法"); form4.Text = "错误"; form4.ShowDialog(); return; } #endregion #region 清空上次请求内容 xtraTabPage1.Controls.Clear(); xtraTabPage2.Controls.Clear(); xtraTabPage3.Controls.Clear(); html = String.Empty; Jhtml = String.Empty; #endregion #region 状态显示 barStaticItem1.Caption = String.Format("当前状态:以{0}方式{1}请求本链接", String.IsNullOrEmpty(richEditControl1.Text) ? "GET" : "POST", thread_flag ? "并发" : "普通"); #endregion #region 配置请求头 info = CreateHttp(); #endregion if (!thread_flag) { //发送请求 html = HttpMethod.HttpWork(ref info); //显示 RichEditControl control1 = new RichEditControl(); control1.Text = "Ret-Cookie:" + info.Cookie.ConventToString() + "\n" + html; control1.ActiveViewType = RichEditViewType.Simple; control1.Dock = DockStyle.Fill; control1.ReadOnly = true; xtraTabPage3.Controls.Add(control1); //渲染 EWebBrowser browser = new EWebBrowser(); browser.DocumentText = html; browser.Dock = DockStyle.Fill; xtraTabPage1.Controls.Add(browser); RichEditControl control2 = new RichEditControl(); if (EWebBrowser.WaitWebPageLoad(browser)) { Jhtml = browser.Document.Body.OuterHtml; } control2.Text = Jhtml; control2.ActiveViewType = RichEditViewType.Simple; control2.Dock = DockStyle.Fill; control2.ReadOnly = true; xtraTabPage2.Controls.Add(control2); barStaticItem1.Caption = String.Format("当前状态:以{0}方式{1}请求完毕", String.IsNullOrEmpty(richEditControl1.Text) ? "GET" : "POST", thread_flag ? "并发" : "普通"); } else { barButtonItem1.Enabled = false; barButtonItem2.Enabled = false; barButtonItem12.Enabled = false; barButtonItem14.Enabled = false; barButtonItem4.Enabled = false; barButtonItem3.Enabled = false; //box1 = new RichTextBox(); //box1.Dock = DockStyle.Fill; //box1.SelectionStart = box1.Text.Length; //box1.SelectionLength = 0; //xtraTabPage3.Controls.Add(box1); control = new RichEditControl(); control.Dock = DockStyle.Fill; control.ReadOnly = true; control.ActiveViewType = RichEditViewType.Simple; xtraTabPage3.Controls.Add(control); ConfigFinishFunc(); } tabPane1.SelectedPage = tabNavigationPage2; }
/// <summary> /// 配置请求头 /// </summary> /// <returns></returns> public HttpInfo CreateHttp() { HttpInfo info = new HttpInfo(); #region 请求头配置 info = new HttpInfo(textEdit1.Text); if (!String.IsNullOrEmpty(richEditControl1.Text)) { var list = RegexMethod.GetMutResult("[\u4e00-\u9fa5]+?", richEditControl1.Text); String newpostdata = richEditControl1.Text; for (int i = 0; i < list.Count; i++) { newpostdata = newpostdata.Replace(list[i], HttpMethod.URLEncode(list[i])); } info.PostData = newpostdata; } info.UseSystemProxy = checkEdit7.Checked; //info.Host = textBox17.Text; info.IgnoreWebException = true; info.User_Agent = textEdit2.Text; info.Referer = textEdit4.Text; info.ContentType = textEdit3.Text; info.Accept = textEdit5.Text; info.AcceptEncoding = textEdit6.Text; info.CheckUrl = checkEdit4.Checked; info.Expect100Continue = checkEdit5.Checked; if (!String.IsNullOrEmpty(textEdit8.Text)) { info.Encoding = Encoding.GetEncoding(textEdit8.Text); } if (!String.IsNullOrEmpty(richEditControl2.Text)) { info.Cookie = new CookieString(richEditControl2.Text, true); } else { info.CC = new CookieContainer(); } info.AllowAutoRedirect = checkEdit1.Checked; info.KeepLive = checkEdit2.Checked; if (checkEdit3.Checked) { info.Header.Add("X-Requested-With", "XMLHttpRequest"); } foreach (var item in HeadDic) { if (item.Key == "Host") { info.Host = item.Value; continue; } if (item.Key == "ProtocolVersion") { if (item.Value == "1.0") { info.ProtocolVersion = ProtocolVersionEnum.V10; } else { info.ProtocolVersion = ProtocolVersionEnum.V11; } continue; } if (item.Key == "proxy") { var arr = item.Value.Split('|'); if (arr.Length > 0) { info.Ip = arr[0]; } if (arr.Length > 1) { info.Proxy_UserName = arr[1]; } if (arr.Length > 2) { info.Proxy_PassWord = arr[2]; } continue; } info.Header.Add(item.Key, item.Value); } #endregion return(info); }
private void RunFunc(string xpathstr, string regstr, string proxyapi_url, bool timeflag, string ip) { //GC.Collect(); UpRichTextBox uptxt = new UpRichTextBox(UpRichTxt); #region 配置请求头 HttpInfo info_goto = CreateHttp(); if (!String.IsNullOrEmpty(ip)) { info_goto.Ip = ip; } #endregion double time = 0; Stopwatch sw = new Stopwatch(); if (timeflag) { sw.Start(); } String Html = HttpMethod.HttpWork(ref info_goto); if (sw.IsRunning) { sw.Stop(); time = sw.Elapsed.TotalSeconds; } String cookie = info_goto.Cookie.ConventToString(); String _ip = info_goto.Ip; #region 正则判断访问结果 if (!String.IsNullOrEmpty(regstr) && String.IsNullOrEmpty(xpathstr)) { String regtxt = RegexMethod.GetSingleResult(regstr, Html); String status = String.Empty; String txt = String.Empty; if (String.IsNullOrEmpty(regtxt)) { status = "访问失败"; } else { status = "访问成功"; } if (timeflag) { if (!String.IsNullOrEmpty(proxyapi_url)) { txt = "返回结果:" + status + "\r\n" + "ret-cookie:" + cookie + "\r\n" + "请求IP:" + _ip + "\r\n" + "请求时间:" + time + "\r\n"; } else { txt = "返回结果:" + status + "\r\n" + "ret-cookie:" + cookie + "\r\n" + "请求时间:" + time + "\r\n"; } } else { if (!String.IsNullOrEmpty(proxyapi_url)) { txt = "返回结果:" + status + "\r\n" + "ret-cookie:" + cookie + "\r\n" + "请求IP:" + _ip + "\r\n"; } else { txt = "返回结果:" + status + "\r\n" + "ret-cookie:" + cookie + "\r\n"; } } object[] arg = { txt }; this.Invoke(uptxt, arg); } #endregion #region Xpath判断访问结果 if (!String.IsNullOrEmpty(xpathstr) && String.IsNullOrEmpty(regstr)) { String regtxt = XpathMethod.GetSingleResult(xpathstr, Html); String status = String.Empty; String txt = String.Empty; if (String.IsNullOrEmpty(regtxt)) { status = "访问失败"; } else { status = "访问成功"; } if (timeflag) { if (!String.IsNullOrEmpty(proxyapi_url)) { txt = "返回结果:" + status + "\r\n" + "ret-cookie:" + cookie + "\r\n" + "请求IP:" + _ip + "\r\n" + "请求时间:" + time + "\r\n"; } else { txt = "返回结果:" + status + "\r\n" + "ret-cookie:" + cookie + "\r\n" + "请求时间:" + time + "\r\n"; } } else { if (!String.IsNullOrEmpty(proxyapi_url)) { txt = "返回结果:" + status + "\r\n" + "ret-cookie:" + cookie + "\r\n" + "请求IP:" + _ip + "\r\n"; } else { txt = "返回结果:" + status + "\r\n" + "ret-cookie:" + cookie + "\r\n"; } } object[] arg = { txt }; this.Invoke(uptxt, arg); } #endregion #region 直接输出结果 if (String.IsNullOrEmpty(regstr) && String.IsNullOrEmpty(xpathstr)) { String txt = String.Empty; if (timeflag) { if (!String.IsNullOrEmpty(proxyapi_url)) { txt = "返回结果:" + Html + "\r\n" + "ret-cookie:" + cookie + "\r\n" + "请求IP:" + _ip + "\r\n" + "请求时间:" + time + "\r\n"; } else { txt = "返回结果:" + Html + "\r\n" + "ret-cookie:" + cookie + "\r\n" + "请求时间:" + time + "\r\n"; } } else { if (!String.IsNullOrEmpty(proxyapi_url)) { txt = "返回结果:" + Html + "\r\n" + "ret-cookie:" + cookie + "\r\n" + "请求IP:" + _ip + "\r\n"; } else { txt = "返回结果:" + Html + "\r\n" + "ret-cookie:" + cookie + "\r\n"; } } object[] arg = { txt }; this.Invoke(uptxt, arg); } #endregion }
private void btnSend_Click(object sender, RoutedEventArgs e) { HttpInfo httpInfo = GetHttpInfo(); GetHttpMessage(httpInfo); }