/// <summary> /// 此流程为从服务器更新cookie /// </summary> /// <returns></returns> bool SyncCookie() { bool syncStatus = false; try { String result = GetServerCookie(); if (!String.IsNullOrEmpty(result)) { LoginInfoSyncResponse infoSyncResponse = JsonConvert.DeserializeObject <LoginInfoSyncResponse>(result); if (infoSyncResponse.data != null && !String.IsNullOrEmpty(infoSyncResponse.data)) { CookieModel = new FiddlerCacheInfo(infoSyncResponse.data); InitProxyUrl(infoSyncResponse.proxyUrl); SaveCookieSign = true; syncStatus = true; } } } catch (Exception) { } return(syncStatus); }
public void FiddlerApplication_BeforeRequest(Fiddler.Session oSession) { lock (fiddlerLockObj) { try { if (CheckFilterRule(oSession)) { oSession.bBufferResponse = true; #region 人寿替换请求 if (oSession.fullUrl.Equals("https://ins.chinalife-p.com.cn/workbench/workbench/login.html")) { oSession.fullUrl = "https://ins.chinalife-p.com.cn/workbench/workbench/index.html"; } #endregion String requestCookie = oSession.oRequest.headers["Cookie"].ToString(); FiddlerCacheInfo cookieRequestModel = new FiddlerCacheInfo(requestCookie); if (CookieModel != null) { //如果全部相等 //if (CookieModel.CookieStr.Equals(cookieRequestModel.CookieStr)) { return; } #region 更新cookie,不能完全替换 int changeCookieSign = 0; foreach (var requestItem in cookieRequestModel.CookieDic) { if (CookieModel.CookieDic.ContainsKey(requestItem.Key) && !String.IsNullOrEmpty(requestItem.Value)) { if (ProxyUrl.Contains("ins.chinalife-p.com.cn")) { if (requestItem.Value.Contains("AlteonP73workbench")) { CookieModel.CookieDic[requestItem.Key] = requestItem.Value; Log(String.Format("fiddler 键值对需要更新:{0}={1}", requestItem.Key, requestItem.Value)); changeCookieSign++; } } else { //CookieModel.CookieDic[requestItem.Key] = requestItem.Value; //SetRichText(String.Format("fiddler 键值对需要更新:{0}={1}", requestItem.Key, requestItem.Value)); //标记需要更新 changeCookieSign++; } } else { CookieModel.CookieDic.Add(requestItem.Key, requestItem.Value); Log(String.Format("fiddler 键值对需要新增:{0}={1}", requestItem.Key, requestItem.Value)); changeCookieSign++; } } if (changeCookieSign > 0) { //生成新的cookieSb StringBuilder newCookieSb = new StringBuilder(); foreach (var item in CookieModel.CookieDic) { newCookieSb.Append(String.Format(";{0}={1}", item.Key, item.Value)); } String newCookieStr = newCookieSb.ToString().Trim(new char[] { ';' }).Trim(); if (!String.IsNullOrEmpty(newCookieStr)) { CookieModel = new FiddlerCacheInfo(newCookieStr); Log(String.Format("fiddler更新Cookie为:{0}", newCookieStr)); } } #endregion if (!oSession.oRequest.headers.Exists("Cookie")) { oSession.oRequest.headers.Add("Cookie", ""); } oSession.oRequest.headers["Cookie"] = CookieModel.CookieStr; Log(String.Format("fiddler修改了请求:{0}", oSession.fullUrl.ToString())); } } } catch (Exception ex) { MessageBox.Show(String.Format("BeforeRequest异常:" + ex.Message)); } } }
private void startProxyButton_Click(object sender, EventArgs e) { try { this.startProxyButton.Enabled = false; Microsoft.Win32.RegistryKey rk = Microsoft.Win32.Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings", true); if (rk.GetValue("ProxyEnable").ToString() == "1") { /** * 1.如果系统代理已经是勾选状态,且判定为代理工具设置的代理,则直接从服务器获取最新cookie,提示已更新即可 * 此场景应该较多 * 2.否则提醒用户关闭后在开启 */ String proxyServer = rk.GetValue("ProxyServer").ToString(); if (!String.IsNullOrEmpty(proxyServer) && proxyServer.Contains(":8899")) { Config.UserName = this.usernameTxt.Text.Trim(); bool syncCookieStatus = SyncCookie(); if (syncCookieStatus) { MessageBox.Show("代理已更新,请执行 CTRL+F5 刷新页面哦!", "提醒"); } else { MessageBox.Show("代理更新失败,请关闭代理后重试,或联系管理员处理!", "提醒"); } } else { MessageBox.Show("代理已经是启动状态哦!,如需重新启动代理,请先点击关闭代理按钮!", "提醒"); } } else { try { Config.UserName = this.usernameTxt.Text.Trim(); if (!String.IsNullOrEmpty(Config.UserName)) { string result = GetServerCookie(); if (!String.IsNullOrEmpty(result)) { LoginInfoSyncResponse infoSyncResponse = JsonConvert.DeserializeObject <LoginInfoSyncResponse>(result); if (infoSyncResponse.data != null && !String.IsNullOrEmpty(infoSyncResponse.data)) { try { CookieModel = new FiddlerCacheInfo(infoSyncResponse.data); InitProxyUrl(infoSyncResponse.proxyUrl); SaveCookieSign = true; FiddlerUtils.SetSSLCer(); Fiddler.FiddlerApplication.BeforeRequest += FiddlerApplication_BeforeRequest; Fiddler.FiddlerApplication.BeforeResponse += FiddlerApplication_BeforeResponse; BiHu.BaoXian.ClassCommon.WebProxy.Start(8899); MessageBox.Show("代理已启动!!! \r\n\r\n\r\n请执行以下步骤 ( 建议使用Chrome浏览器 ^_^ ) :\r\n\r\n\r\n1.打开浏览器。\r\n\r\n2.执行 Ctrl + Shift + Delete 清空缓存。\r\n\r\n3.重启浏览器,直接进入核心系统首页即可。", "提醒"); } catch (Exception ex) { throw new Exception(String.Format("启动代理发生异常,请与管理员联系!\r\n\r\n({0})\r\n\r\n注:可点击【关闭代理】按钮,再试一次哦!", ex.Message)); } } else { MessageBox.Show("工号信息失效,请与管理员联系!", "提醒"); } } else { MessageBox.Show("工号信息为空,请与管理员联系!", "提醒"); } } else { MessageBox.Show("请输入核心系统工号哦!", "提醒"); this.usernameTxt.Focus(); } } catch (Exception ex) { MessageBox.Show(String.Format("启动代理发生异常:{0}\r\n\r\n注:可点击【关闭代理】按钮,再试一次哦!", ex.Message), "提醒"); } } } catch (Exception) { } finally { this.startProxyButton.Enabled = true; } }