public void GetCookies_AddCookieVersion0WithExplicitDomain_CookieReturnedForDomainAndSubdomains() { const string SchemePrefix = "http://"; const string OriginalDomain = "contoso.com"; var container = new CookieContainer(); var cookie1 = new Cookie(CookieName1, CookieValue1) { Domain = OriginalDomain }; container.Add(new Uri(SchemePrefix + OriginalDomain), cookie1); var uri = new Uri(SchemePrefix + OriginalDomain); var cookies = container.GetCookies(uri); Assert.Equal(1, cookies.Count); Assert.Equal(OriginalDomain, cookies[CookieName1].Domain); uri = new Uri(SchemePrefix + "www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(1, cookies.Count); uri = new Uri(SchemePrefix + "x.www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(1, cookies.Count); uri = new Uri(SchemePrefix + "y.x.www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(1, cookies.Count); uri = new Uri(SchemePrefix + "z.y.x.www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(1, cookies.Count); }
public void GetCookies_AddCookiesWithImplicitDomain_CookiesReturnedOnlyForExactDomainMatch() { const string SchemePrefix = "http://"; const string OriginalDomain = "contoso.com"; var container = new CookieContainer(); var cookie1 = new Cookie(CookieName1, CookieValue1); var cookie2 = new Cookie(CookieName2, CookieValue2) { Version = 1 }; var uri = new Uri(SchemePrefix + OriginalDomain); container.Add(uri, cookie1); container.Add(uri, cookie2); var cookies = container.GetCookies(uri); Assert.Equal(2, cookies.Count); Assert.Equal(OriginalDomain, cookies[CookieName1].Domain); Assert.Equal(OriginalDomain, cookies[CookieName2].Domain); uri = new Uri(SchemePrefix + "www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(0, cookies.Count); uri = new Uri(SchemePrefix + "x.www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(0, cookies.Count); uri = new Uri(SchemePrefix + "y.x.www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(0, cookies.Count); uri = new Uri(SchemePrefix + "z.y.x.www." + OriginalDomain); cookies = container.GetCookies(uri); Assert.Equal(0, cookies.Count); }
private void ReadAuthCookie() { var responseCookies = cookieContainer?.GetCookies(new Uri(BarboraUrl))?.Cast <Cookie>(); var authCookie = responseCookies?.Where(x => x.Name == AuthCookieName)?.FirstOrDefault(); if (authCookie == null) { throw new SecurityException(string.Format("Auth cookie \"{0}\" was not found", AuthCookieName)); } SetAuthCookie(authCookie); }
public Cookie GetJWTSession(Uri connectUri, string token, string cookieName = "X-Qlik-Session") { try { var newUri = new UriBuilder(connectUri); newUri.Path = $"{newUri.Path.Trim('/')}/sense/app"; logger.Debug($"ConnectUri: {connectUri}"); var fullConnectUri = newUri.Uri; logger.Debug($"Connection to uri: {fullConnectUri}"); var cookieContainer = new CookieContainer(); var connectionHandler = new HttpClientHandler { UseDefaultCredentials = true, CookieContainer = cookieContainer, }; connectionHandler.ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { if (ServerCertificateValidation.Validate(sender, certificate, sslPolicyErrors)) { return(true); } ServerCertificateValidation.ReadAlternativeDnsNames(connectUri, certificate); return(false); }; var connection = new HttpClient(connectionHandler); connection.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}"); var message = connection.GetAsync(fullConnectUri).Result; logger.Trace($"Message: {message}"); var responseCookies = cookieContainer?.GetCookies(fullConnectUri)?.Cast <Cookie>() ?? null; var cookie = responseCookies.FirstOrDefault(c => c.Name.Equals(cookieName)) ?? null; logger.Debug($"The session cookie was found. {cookie?.Name} - {cookie?.Value}"); return(cookie); } catch (Exception ex) { logger.Error(ex, "Can´t create session cookie with JWT."); return(null); } }
/// <summary> /// 使用Post方法获取字符串结果 /// </summary> /// <param name="url"></param> /// <param name="cookieContainer"></param> /// <param name="postStream"></param> /// <param name="fileDictionary">需要上传的文件,Key:对应要上传的Name,Value:本地文件名</param> /// <param name="timeOut"></param> /// <param name="checkValidationResult">验证服务器证书回调自动验证</param> /// <returns></returns> public static string HttpPost(string url, CookieContainer cookieContainer = null, Stream postStream = null, Dictionary <string, string> fileDictionary = null, string refererUrl = null, Encoding encoding = null, int timeOut = Config.TIME_OUT, bool checkValidationResult = false) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.Timeout = timeOut; if (checkValidationResult) { ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult); } #region 处理Form表单文件上传 var formUploadFile = fileDictionary != null && fileDictionary.Count > 0;//是否用Form上传文件 if (formUploadFile) { //通过表单上传文件 postStream = postStream ?? new MemoryStream(); string boundary = "----" + DateTime.Now.Ticks.ToString("x"); //byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); string fileFormdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n"; string dataFormdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; foreach (var file in fileDictionary) { try { var fileName = file.Value; //准备文件流 using (var fileStream = FileHelper.GetFileStream(fileName)) { string formdata = null; if (fileStream != null) { //存在文件 formdata = string.Format(fileFormdataTemplate, file.Key, /*fileName*/ Path.GetFileName(fileName)); } else { //不存在文件或只是注释 formdata = string.Format(dataFormdataTemplate, file.Key, file.Value); } //统一处理 var formdataBytes = Encoding.ASCII.GetBytes(postStream.Length == 0 ? formdata.Substring(2, formdata.Length - 2) : formdata);//第一行不需要换行 postStream.Write(formdataBytes, 0, formdataBytes.Length); //写入文件 if (fileStream != null) { byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { postStream.Write(buffer, 0, bytesRead); } } } } catch (Exception ex) { throw ex; } } //结尾 var footer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); postStream.Write(footer, 0, footer.Length); request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); } else { request.ContentType = "application/x-www-form-urlencoded"; } #endregion request.ContentLength = postStream != null ? postStream.Length : 0; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; request.KeepAlive = true; if (!string.IsNullOrEmpty(refererUrl)) { request.Referer = refererUrl; } request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"; if (cookieContainer != null) { request.CookieContainer = cookieContainer; } #region 输入二进制流 if (postStream != null) { postStream.Position = 0; //直接写入流 Stream requestStream = request.GetRequestStream( ); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = postStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); } //debug postStream.Seek(0, SeekOrigin.Begin); StreamReader sr = new StreamReader(postStream); var postStr = sr.ReadToEnd(); postStream.Close();//关闭文件访问 } #endregion HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (cookieContainer != null) { response.Cookies = cookieContainer.GetCookies(response.ResponseUri); } using (Stream responseStream = response.GetResponseStream()) { using (StreamReader myStreamReader = new StreamReader(responseStream, encoding ?? Encoding.GetEncoding("utf-8"))) { string retString = myStreamReader.ReadToEnd(); return(retString); } } }
protected override void _Login(LoginParamValue[] LoginParams) { string APIKey = ""; foreach (LoginParamValue x in LoginParams) { if (x.Param.Name.ToLower() == "api key") { APIKey = x.Value; } } Cookies = new CookieContainer(); ClientHandlr = new HttpClientHandler { UseCookies = true, CookieContainer = Cookies, AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip };; Client = new HttpClient(ClientHandlr) { BaseAddress = new Uri("https://ethercrash.io") }; Client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"); Client.DefaultRequestHeaders.Add("referer", "https://www.ethercrash.io/play"); Client.DefaultRequestHeaders.Add("origin", "https://www.ethercrash.io"); try { Cookies.Add(new Cookie("id", APIKey, "/", "ethercrash.io")); string Response = Client.GetStringAsync("https://www.ethercrash.io/play").Result; Response = Client.GetStringAsync("https://www.ethercrash.io/socket.io/?EIO=3&transport=polling&t=" + json.CurrentDate()).Result; Response = Client.GetStringAsync("https://gs.ethercrash.io/socket.io/?EIO=3&transport=polling&t=" + json.CurrentDate()).Result; string iochat = ""; foreach (Cookie c3 in Cookies.GetCookies(new Uri("http://www.ethercrash.io"))) { if (c3.Name == "io") { iochat = c3.Value; } if (c3.Name == "__cfduid") { cfuid = c3.Value; } } Response = Client.GetStringAsync("https://www.ethercrash.io/socket.io/?EIO=3&sid=" + iochat + "&transport=polling&t=" + json.CurrentDate()).Result; foreach (Cookie c3 in Cookies.GetCookies(new Uri("http://gs.ethercrash.io"))) { if (c3.Name == "io") { io = c3.Value; } if (c3.Name == "__cfduid") { cfuid = c3.Value; } } StringContent ottcontent = new StringContent(""); HttpResponseMessage RespMsg = Client.PostAsync("https://www.ethercrash.io/ott", ottcontent).Result; Response = RespMsg.Content.ReadAsStringAsync().Result; if (RespMsg.IsSuccessStatusCode) { ott = Response; } else { callLoginFinished(false); return; } string body = "420[\"join\",{\"ott\":\"" + ott + "\",\"api_version\":1}]"; body = body.Length + ":" + body; StringContent stringContent = new StringContent(body, UnicodeEncoding.UTF8, "text/plain"); Response = Client.PostAsync("https://gs.ethercrash.io/socket.io/?EIO=3&sid=" + io + "&transport=polling&t=" + json.CurrentDate(), stringContent).Result.Content.ReadAsStringAsync().Result; body = "420[\"join\",[\"english\"]]"; body = body.Length + ":" + body; StringContent stringContent2 = new StringContent(body, UnicodeEncoding.UTF8, "text/plain"); Response = Client.PostAsync("https://www.ethercrash.io/socket.io/?EIO=3&sid=" + iochat + "&transport=polling&t=" + json.CurrentDate(), stringContent2).Result.Content.ReadAsStringAsync().Result; Response = Client.GetStringAsync("https://www.ethercrash.io/socket.io/?EIO=3&sid=" + iochat + "&transport=polling&t=" + json.CurrentDate()).Result; List <KeyValuePair <string, string> > cookies = new List <KeyValuePair <string, string> >(); cookies.Add(new KeyValuePair <string, string>("__cfduid", cfuid)); cookies.Add(new KeyValuePair <string, string>("io", iochat)); cookies.Add(new KeyValuePair <string, string>("id", APIKey)); ChatSock = new WebSocket("wss://www.ethercrash.io/socket.io/?EIO=3&transport=websocket&sid=" + iochat, null, cookies /*, * null, * "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36", * "https://www.ethercrash.io", * WebSocketVersion.None*/); ChatSock.Opened += Sock_Opened; ChatSock.Error += Sock_Error; ChatSock.MessageReceived += Sock_MessageReceived; ChatSock.Closed += Sock_Closed; ChatSock.Open(); while (ChatSock.State == WebSocketState.Connecting) { Thread.Sleep(300); //Response = Client.GetStringAsync("https://gs.ethercrash.io/socket.io/?EIO=3&sid=" + io + "&transport=polling&t=" + json.CurrentDate()).Result; } if (ChatSock.State == WebSocketState.Open) { } else { callLoginFinished(false); return; } //Response = Client.GetStringAsync("https://gs.ethercrash.io/socket.io/?EIO=3&sid=" + io + "&transport=polling&t=" + json.CurrentDate()).Result; Thread.Sleep(200); //Response = Client.GetStringAsync("https://gs.ethercrash.io/socket.io/?EIO=3&sid=" + io + "&transport=polling&t=" + json.CurrentDate()).Result; List <KeyValuePair <string, string> > cookies2 = new List <KeyValuePair <string, string> >(); cookies2.Add(new KeyValuePair <string, string>("__cfduid", cfuid)); cookies2.Add(new KeyValuePair <string, string>("io", io)); cookies2.Add(new KeyValuePair <string, string>("id", APIKey)); List <KeyValuePair <string, string> > headers = new List <KeyValuePair <string, string> >(); headers.Add(new KeyValuePair <string, string>("Host", "gs.ethercrash.io")); headers.Add(new KeyValuePair <string, string>("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36")); headers.Add(new KeyValuePair <string, string>("Origin", "https://www.ethercrash.io")); Sock = new WebSocket("wss://gs.ethercrash.io/socket.io/?EIO=3&transport=websocket&sid=" + io /*, * null, * cookies2, * headers, * "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36", * "https://www.ethercrash.io", * WebSocketVersion.None*/); Sock.Opened += Sock_Opened; Sock.Error += Sock_Error; Sock.MessageReceived += Sock_MessageReceived; Sock.Closed += Sock_Closed; Sock.Open(); while (Sock.State == WebSocketState.Connecting) { Thread.Sleep(300); //Response = Client.GetStringAsync("https://gs.ethercrash.io/socket.io/?EIO=3&sid=" + io + "&transport=polling&t=" + json.CurrentDate()).Result; } if (Sock.State == WebSocketState.Open) { callLoginFinished(true); isec = true; Thread t = new Thread(pingthread); t.Start(); return; } callLoginFinished(false); } catch (Exception ex) { Logger.DumpLog(ex.ToString(), -1); callLoginFinished(false); } }
/// <summary> /// Gets a <see cref="System.Net.CookieCollection"/> that contains the <see cref="System.Net.Cookie"/> instances that are associated with a specific <see cref="Uri"/>. /// </summary> /// <param name="uri">The URI of the System.Net.Cookie instances desired.</param> /// <returns>A <see cref="System.Net.CookieCollection"/> that contains the <see cref="System.Net.Cookie"/> instances that are associated with a specific <see cref="Uri"/>.</returns> public CookieCollection GetCookies(Uri uri) { return(CookieContainer.GetCookies(uri)); }
/// <summary> /// 下载工作的具体实现 /// </summary> /// <param name="request">请求信息</param> /// <param name="spider">爬虫</param> /// <returns>页面数据</returns> protected override Task <Page> DowloadContent(Request request, ISpider spider) { Site site = spider.Site; try { lock (_locker) { if (_webDriver == null) { _webDriver = WebDriverUtil.Open(_browser, _option); if (_domains != null) { foreach (var domain in _domains) { var cookies = CookieContainer.GetCookies(new Uri(domain)); foreach (System.Net.Cookie cookie in cookies) { AddCookieToDownloadClient(cookie); } } } if (!_isLogined && CookieInjector != null) { if (CookieInjector is WebDriverLoginHandler webdriverLoginHandler) { webdriverLoginHandler.Driver = _webDriver as RemoteWebDriver; } CookieInjector.Inject(this, spider); _isLogined = true; } } } //#if NET_CORE // string query = string.IsNullOrEmpty(uri.Query) ? "" : $"?{WebUtility.UrlEncode(uri.Query.Substring(1, uri.Query.Length - 1))}"; //#else // string query = string.IsNullOrEmpty(uri.Query) ? "" : $"?{HttpUtility.UrlPathEncode(uri.Query.Substring(1, uri.Query.Length - 1))}"; //#endif // string realUrl = $"{uri.Scheme}://{uri.DnsSafeHost}{(uri.Port == 80 ? "" : ":" + uri.Port)}{uri.AbsolutePath}{query}"; // var domainUrl = // $"{request.Uri.Scheme}://{request.Uri.DnsSafeHost}{(request.Uri.Port == 80 ? "" : ":" + request.Uri.Port)}"; string realUrl = request.Url; NetworkCenter.Current.Execute("webdriver-download", () => { _webDriver.Navigate().GoToUrl(realUrl); if (WebDriverHandlers != null) { foreach (var handler in WebDriverHandlers) { handler.Handle((RemoteWebDriver)_webDriver); } } }); Thread.Sleep(_webDriverWaitTime); Page page = new Page(request) { Content = _webDriver.PageSource, TargetUrl = _webDriver.Url }; return(Task.FromResult(page)); } catch (DownloadException de) { Page page = new Page(request) { Exception = de }; if (site.CycleRetryTimes > 0) { page = site.AddToCycleRetry(request); } spider.Logger.Error($"下载 {request.Url} 失败: {de.Message}."); return(Task.FromResult(page)); } catch (Exception e) { spider.Logger.Error($"下载 {request.Url} 失败: {e.Message}."); Page page = new Page(request) { Exception = e }; return(Task.FromResult(page)); } }
private static void VerifyGetCookies(CookieContainer cc1, Uri uri, Cookie[] expected) { CookieCollection cc2 = cc1.GetCookies(uri); Assert.Equal(expected.Length, cc2.Count); for (int i = 0; i < expected.Length; i++) { Cookie c1 = expected[i]; Cookie c2 = cc2[i]; Assert.Equal(c1.Name, c2.Name); // Primitive check for equality Assert.Equal(c1.Value, c2.Value); } }
protected virtual async Task <OctopusResponse <TResponseResource> > DispatchRequest <TResponseResource>(OctopusRequest request, bool readResponse) { using (var message = new HttpRequestMessage()) { message.RequestUri = request.Uri; message.Method = new HttpMethod(request.Method); if (request.Method == "PUT" || request.Method == "DELETE") { message.Method = HttpMethod.Post; message.Headers.Add("X-HTTP-Method-Override", request.Method); } if (RootDocument != null) { var expectedCookieName = $"{ApiConstants.AntiforgeryTokenCookiePrefix}_{RootDocument.InstallationId}"; var antiforgeryCookie = cookieContainer.GetCookies(cookieOriginUri) .Cast <Cookie>() .SingleOrDefault(c => string.Equals(c.Name, expectedCookieName)); if (antiforgeryCookie != null) { message.Headers.Add(ApiConstants.AntiforgeryTokenHttpHeaderName, antiforgeryCookie.Value); } } SendingOctopusRequest?.Invoke(request); BeforeSendingHttpRequest?.Invoke(message); if (request.RequestResource != null) { message.Content = GetContent(request); } var completionOption = readResponse ? HttpCompletionOption.ResponseContentRead : HttpCompletionOption.ResponseHeadersRead; try { using (var response = await client.SendAsync(message, completionOption).ConfigureAwait(false)) { AfterReceivedHttpResponse?.Invoke(response); if (!response.IsSuccessStatusCode) { throw await OctopusExceptionFactory.CreateException(response).ConfigureAwait(false); } var resource = readResponse ? await ReadResponse <TResponseResource>(response).ConfigureAwait(false) : default(TResponseResource); var locationHeader = response.Headers.Location?.OriginalString; var octopusResponse = new OctopusResponse <TResponseResource>(request, response.StatusCode, locationHeader, resource); ReceivedOctopusResponse?.Invoke(octopusResponse); return(octopusResponse); } } catch (TaskCanceledException) { throw new TimeoutException($"Timeout getting response, client timeout is set to {client.Timeout}."); } } }
public static async Task <HTTPResponse> Request(string url, HttpMethod method, HttpContent param, IDictionary <string, string> headers, IDictionary <string, string> cookies, CancellationToken cancellationToken) { var result = new HTTPResponse() { IsOK = false, }; url = Uri.EscapeUriString(Uri.UnescapeDataString(url)); if (method == null) { method = HttpMethod.Get; } var request = new HttpRequestMessage(method, new Uri(url)); Logger.Debug(string.Format(CultureInfo.InvariantCulture, "Requesting {1} \"{0}\"", request.RequestUri.AbsoluteUri, method.Method)); request.Headers.TryAddWithoutValidation("User-Agent", GetUserAgent()); if (param != null) { request.Content = param; } if (headers != null) { foreach (var header in headers) { request.Headers.TryAddWithoutValidation(header.Key, header.Value); } } if (cookies != null) { foreach (var cookie in cookies) { CookieContainer.Add(request.RequestUri, new Cookie(cookie.Key, cookie.Value)); } } HttpResponseMessage response = null; try { response = await Http.SendAsync(request, cancellationToken).ConfigureAwait(false); } catch (Exception e) { Logger.Error($"Request error: {e.Message}"); await Analytics.Send(url, null, null, null, null, null, e, cancellationToken).ConfigureAwait(false); } if (response != null) { result.IsOK = response.IsSuccessStatusCode; #if __EMBY__ result.Content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); result.ContentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false); #else result.Content = await response.Content.ReadAsStringAsync(cancellationToken).ConfigureAwait(false); result.ContentStream = await response.Content.ReadAsStreamAsync(cancellationToken).ConfigureAwait(false); #endif result.Headers = response.Headers; result.Cookies = CookieContainer.GetCookies(request.RequestUri).Cast <Cookie>(); } return(result); }
/// <summary> /// 使用Post方法获取字符串结果 /// </summary> /// <param name="url"></param> /// <param name="cookieContainer"></param> /// <param name="postStream"></param> /// <param name="fileDictionary">需要上传的文件,Key:对应要上传的Name,Value:本地文件名</param> /// <param name="refererUrl"> </param> /// <param name="encoding"> </param> /// <returns></returns> public static string HttpPost(string url, CookieContainer cookieContainer = null, Stream postStream = null, Dictionary <string, string> fileDictionary = null, string refererUrl = null, Encoding encoding = null) { //HttpClient client= new HttpClient(); //client.PostAsync(url, new StreamContent(postStream), new FormUrlEncodedMediaTypeFormatter()); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ProtocolVersion = HttpVersion.Version10; // 这两个代码加上去了 http://www.baiwanzhan.com/site/t121032/ request.Timeout = Config.TimeOut; request.ReadWriteTimeout = Config.TimeOut; // http://blog.csdn.net/zhuyu19911016520/article/details/47948101 ServicePointManager.DefaultConnectionLimit = 50; #region 处理Form表单文件上传 var formUploadFile = fileDictionary != null && fileDictionary.Count > 0;//是否用Form上传文件 if (formUploadFile) { //通过表单上传文件 postStream = new MemoryStream(); string boundary = "----" + DateTime.Now.Ticks.ToString("x"); //byte[] boundarybytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: application/octet-stream\r\n\r\n"; //foreach (var file in fileDictionary) //{ // try // { // var fileName = file.Value; // //准备文件流 // using (var fileStream = FileHelper.GetFileStream(fileName)) // { // var formdata = string.Format(formdataTemplate, file.Key, fileName /*Path.GetFileName(fileName)*/); // var formdataBytes = Encoding.ASCII.GetBytes(postStream.Length == 0 ? formdata.Substring(2, formdata.Length - 2) : formdata);//第一行不需要换行 // postStream.Write(formdataBytes, 0, formdataBytes.Length); // //写入文件 // byte[] buffer = new byte[1024]; // int bytesRead = 0; // while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) // { // postStream.Write(buffer, 0, bytesRead); // } // } // } // catch (Exception ex) // { // throw ex; // } //} //结尾 var footer = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); postStream.Write(footer, 0, footer.Length); request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); } else { request.ContentType = "application/x-www-form-urlencoded"; } #endregion request.ContentLength = postStream != null ? postStream.Length : 0; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; request.KeepAlive = false; if (!string.IsNullOrEmpty(refererUrl)) { request.Referer = refererUrl; } request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"; if (cookieContainer != null) { request.CookieContainer = cookieContainer; } #region 输入二进制流 if (postStream != null) { postStream.Position = 0; //直接写入流 Stream requestStream = request.GetRequestStream(); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = postStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); } postStream.Close();//关闭文件访问 } #endregion // Response一定要释放 using (var response = (HttpWebResponse)request.GetResponse()) { if (cookieContainer != null) { response.Cookies = cookieContainer.GetCookies(response.ResponseUri); } using (Stream responseStream = response.GetResponseStream()) { using (StreamReader myStreamReader = new StreamReader(responseStream, encoding ?? Encoding.GetEncoding("utf-8"))) { string retString = myStreamReader.ReadToEnd(); return(retString); } } } }
private async Task <Dictionary <string, Cookie> > LoginAsync( Cookie antiScrappingCookie, string url, string username, string password, bool unattended) { // TODO: Improve this. var storeSlug = UriHelper.GetStoreSlug(url); var storeCaptchaFilePath = $"captchas/{storeSlug}.jpg"; var storeCaptchaSolutionFilePath = $"captchas/{storeSlug}.txt"; Log.Information("Authenticating in TuEnvio as {username}", username); var signInUrl = url.Replace("/Products?depPid=0", "/signin.aspx"); var captchaUrl = url.Replace("/Products?depPid=0", "/captcha.ashx"); var cookieContainer = new CookieContainer(); string captchaFilePath = null; var captchaText = string.Empty; var isAuthenticated = false; var attempts = 0; CookieCollection httpHandlerCookieCollection = null; do { attempts++; var httpMessageHandler = new HttpClientHandler { CookieContainer = cookieContainer }; if (antiScrappingCookie != null) { cookieContainer.Add(ScraperConfigurations.CookieCollectionUrl, antiScrappingCookie); } var httpClient = new HttpClient(httpMessageHandler) { Timeout = ScraperConfigurations.HttpClientTimeout }; httpClient.DefaultRequestHeaders.CacheControl = new CacheControlHeaderValue { NoCache = true }; httpClient.DefaultRequestHeaders.TryAddWithoutValidation( "user-agent", ScraperConfigurations.GetSupportedAgent()); var browsingContext = BrowsingContext.New(Configuration.Default.WithJs()); IDocument signinPageDocument = null; try { var httpResponseMessage = await httpClient.GetCaptchaSaveAsync(signInUrl); if (httpResponseMessage?.Content != null) { var signinPageContent = await httpResponseMessage.Content.ReadAsStringAsync(); signinPageDocument = await browsingContext.OpenAsync(req => req.Content(signinPageContent)); } } catch (Exception e) { Log.Warning(e, "Error retrieving sign in page with url '{Url}'", signInUrl); } Dictionary <string, string> signInParameters = null; try { signInParameters = await BuildSignInParametersAsync(username, password, signinPageDocument); } catch (Exception e) { Log.Warning(e, "Error building sign in parameters for '{Url}'", signInUrl); } if (signInParameters != null) { var captchaImg = signinPageDocument.QuerySelector <IElement>("#cphPage_Login_captch"); var captchaImgSrc = captchaImg?.Attributes["src"]?.Value; if (!string.IsNullOrWhiteSpace(captchaImgSrc)) { captchaUrl = captchaUrl.Replace("captcha.ashx", captchaImgSrc); } captchaFilePath = await DownloadCaptchaAsync(httpClient, captchaUrl); if (!string.IsNullOrWhiteSpace(captchaFilePath) && File.Exists(captchaFilePath)) { if (unattended) { captchaText = GetCaptchaText(captchaFilePath); } else { File.Delete(storeCaptchaSolutionFilePath); File.Copy(captchaFilePath, storeCaptchaFilePath, true); while (File.Exists(storeCaptchaFilePath) && !File.Exists(storeCaptchaSolutionFilePath)) { await Task.Delay(1000); } captchaText = await File.ReadAllTextAsync(storeCaptchaSolutionFilePath); File.Delete(storeCaptchaFilePath); File.Delete(storeCaptchaSolutionFilePath); } if (!string.IsNullOrWhiteSpace(captchaText)) { signInParameters.Add("ctl00$cphPage$Login$capcha", captchaText); try { await httpClient.PostAsync(signInUrl, new FormUrlEncodedContent(signInParameters)); httpHandlerCookieCollection = cookieContainer.GetCookies(ScraperConfigurations.CookieCollectionUrl); isAuthenticated = !string.IsNullOrWhiteSpace(httpHandlerCookieCollection["ShopMSAuth"]?.Value); } catch (Exception e) { Log.Warning(e, "Error authenticating in '{Url}'", signInUrl); } } } try { if (!isAuthenticated) { File.Delete(captchaFilePath); } } catch (Exception e) { Log.Warning(e, "Error deleting captcha file '{FilePath}'", captchaFilePath); } } }while (attempts < 5 && !isAuthenticated); if (isAuthenticated) { try { File.Move(captchaFilePath, $"captchas/{captchaText}.jpg", true); } catch (Exception e) { Log.Warning(e, "Error moving captcha file {FilePath}", captchaFilePath); } } var cookiesCollection = new Dictionary <string, Cookie>(); if (httpHandlerCookieCollection != null) { foreach (Cookie cookie in httpHandlerCookieCollection) { if (!string.IsNullOrWhiteSpace(cookie.Value)) { cookiesCollection[cookie.Name] = cookie; } } } if (antiScrappingCookie != null) { cookiesCollection[antiScrappingCookie.Name] = antiScrappingCookie; } return(cookiesCollection); }
public Cookie GetCookie(Uri uri, string name) { return(UseCookie ? _cookieContainer.GetCookies(uri)[name] : null); }
public static string GetContentByIndex(string url, int timeout, CookieContainer cc, ref Encoding encoding, out string Rurl, ref CookieCollection cookiesColl, out CookieCollection cookiesCollection) { int n = new Random().Next(0, arayList.Length - 1); Uagent = arayList[n].ToString(); string responsestr = ""; System.GC.Collect(); HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; req.Method = "GET"; req.UserAgent = Uagent; //"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36"; req.Proxy = null; req.KeepAlive = true; req.Timeout = timeout; req.ReadWriteTimeout = timeout; req.CookieContainer = new CookieContainer(); req.CookieContainer.Add(cookiesColl); //req.Headers.Add(HttpRequestHeader.Cookie, ""); // req.Headers.Add(HttpRequestHeader.Cookie, cookies); req.ProtocolVersion = HttpVersion.Version11; // req.Host = "weixin.sogou.com"; try { using (HttpWebResponse response = req.GetResponse() as HttpWebResponse) { cookiesCollection = req.CookieContainer.GetCookies(req.RequestUri); MemoryStream memStream = new MemoryStream();; using (Stream stream = response.GetResponseStream()) { byte[] buffer = new byte[1024]; int byteCount; do { byteCount = stream.Read(buffer, 0, buffer.Length); memStream.Write(buffer, 0, byteCount); } while (byteCount > 0); } var charset = GetEncoding(response.CharacterSet); if (charset == null) { responsestr = Encoding.UTF8.GetString(memStream.ToArray()); var charsetStr = ""; var charsetReg = new System.Text.RegularExpressions.Regex("<meta [^>]*charset=(.*?)(?=(;|\b|\"))"); var match = charsetReg.Match(responsestr); if (match.Groups.Count > 1) { charsetStr = match.Groups[1].Value; if (charsetStr.Trim().ToLower() == "gbk" || charsetStr.Trim().ToLower() == "gb2312") { responsestr = Encoding.GetEncoding("gb2312").GetString(memStream.ToArray()); encoding = Encoding.GetEncoding("gb2312"); } } } else { responsestr = charset.GetString(memStream.ToArray()); encoding = Encoding.GetEncoding("utf-8"); } Rurl = response.ResponseUri.AbsoluteUri; response.Cookies = cc.GetCookies(req.RequestUri); StringBuilder cookieData = new StringBuilder(); Uri uurl = req.RequestUri; string mycc = response.Headers["set-cookie"]; string gfgf = req.Headers["Cookie"]; mycc += ";" + gfgf; response.Close(); } } catch (Exception ex) { // throw; Rurl = url; cookiesCollection = req.CookieContainer.GetCookies(req.RequestUri); Console.WriteLine(DateTime.Now + " " + ex.Message); } finally { if (req != null) { req.Abort(); } } return(responsestr); }
static void Main(string[] args) { var postData = $"userName={username}&password={username}&Id=test"; var loginUrl = ConfigurationManager.AppSettings["loginUrl"]; var testUrl = ConfigurationManager.AppSettings["testUrl"]; var _loginCookies = new CookieContainer(); string stepTwoPostData = ""; using (var loginResponse = WebClientHelper.Post(loginUrl, postData, _loginCookies)) { if (loginResponse != null) { var resp = loginResponse as HttpWebResponse; if (resp != null && resp.StatusCode == HttpStatusCode.OK) { using (var reader = new StreamReader(loginResponse.GetResponseStream())) { HtmlDocument doc = new HtmlDocument(); doc.Load(reader); HtmlNode formNode = doc.DocumentNode.Descendants("form") .First(el => (el.Attributes["name"] != null && el.Attributes["name"].Value == "hiddenform")); if (formNode != null) { var hiddenPostInputs = doc.DocumentNode.Descendants("input").Where(el => el.Attributes["type"] != null && el.Attributes["type"].Value == "hidden" && el.Attributes["value"] != null ); foreach (var input in hiddenPostInputs) { if (!string.IsNullOrEmpty(stepTwoPostData)) { stepTwoPostData += "&"; } stepTwoPostData += $"{input.Attributes["name"].Value}={HttpUtility.UrlEncode(WebUtility.HtmlDecode(input.Attributes["value"].Value))}"; } } } } else if (resp != null && resp.StatusCode == HttpStatusCode.Found) { var location = resp.Headers["Location"]; if (location.Clear() != null) { Uri uri = new Uri(location.Clear()); var status = HttpUtility.ParseQueryString(uri.Query)["status"]; if (status.Clear() != null) { throw new Exception($"Login Error. Status: {status.Clear()}"); } } } else { throw new Exception("System Error"); } } } if (stepTwoPostData.Clear() == null) { throw new Exception("Unknown Error"); } _loginCookies = new CookieContainer(); using (var testResponse = WebClientHelper.Post(testUrl, stepTwoPostData, _loginCookies)) { var httpResponse = testResponse as HttpWebResponse; if (!httpResponse.StatusCode.In(HttpStatusCode.Found, HttpStatusCode.OK)) { throw new Exception("Invalid Status Code"); } if (!httpResponse.ResponseUri.ToString().StartsWith(testUrl, StringComparison.CurrentCultureIgnoreCase)) { throw new Exception("Invalid Response Uri"); } using (StreamReader reader = new StreamReader(httpResponse.GetResponseStream())) { var alltext = reader.ReadToEnd().ToString(); foreach (var cookie in _loginCookies.GetCookies(new Uri(testUrl))) { var txt = cookie.ToString(); } } } }
public static async void Add_ReachedMaxCountWithExpiredCookies_Added() { Cookie c1 = new Cookie("name1", "value", "", ".domain1.com"); Cookie c2 = new Cookie("name2", "value", "", ".domain2.com"); Cookie c3 = new Cookie("name3", "value", "", ".domain3.com"); c1.Expires = DateTime.Now.AddSeconds(1); // The cookie will expire in 1 second CookieContainer cc = new CookieContainer(2); cc.Add(c1); cc.Add(c2); await Task.Delay(2000); // Sleep for 2 seconds to wait for the cookie to expire cc.Add(c3); Assert.Equal(0, cc.GetCookies(new Uri("http://domain1.com")).Count); Assert.Equal(c3, cc.GetCookies(new Uri("http://domain3.com"))[0]); }
public void GetCookies_NonExistent_NoResults() { CookieContainer cc = CreateCount11Container(); Assert.Equal(0, cc.GetCookies(new Uri("http://non.existent.uri.com")).Count); }
public static async void GetCookies_RemovesExpired_Cookies() { Cookie c1 = new Cookie("name1", "value", "", ".url1.com"); Cookie c2 = new Cookie("name2", "value", "", ".url2.com"); c1.Expires = DateTime.Now.AddSeconds(1); // The cookie will expire in 1 second CookieContainer cc = new CookieContainer(); cc.Add(c1); cc.Add(c2); await Task.Delay(2000); // Sleep for 2 seconds to wait for the cookie to expire Assert.Equal(0, cc.GetCookies(new Uri("http://url1.com")).Count); // There should no longer be such a cookie }
public static void GetCookies_Invalid() { CookieContainer cc = new CookieContainer(); Assert.Throws <ArgumentNullException>(() => cc.GetCookies(null)); }
// This code block executes each time a new document is loaded into the web browser control private void wbAuth_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { // Find the page header, and remove it. This gives the login form a more streamlined look. dynamic htmldoc = wbAuth.Document.DomDocument; dynamic globalHeader = htmldoc.GetElementById("global_header"); if (globalHeader != null) { try { globalHeader.parentNode.removeChild(globalHeader); } catch (Exception) { } } // Get the URL of the page that just finished loading var url = wbAuth.Url.AbsoluteUri; // If the page it just finished loading is the login page if (url == "https://steamcommunity.com/login/home/?goto=my/profile" || url == "https://store.steampowered.com/login/transfer" || url == "https://store.steampowered.com//login/transfer") { // Get a list of cookies from the current page CookieContainer container = GetUriCookieContainer(wbAuth.Url); var cookies = container.GetCookies(wbAuth.Url); foreach (Cookie cookie in cookies) { if (cookie.Name.StartsWith("steamMachineAuth")) { Settings.Default.steamMachineAuth = cookie.Value; } } } // If the page it just finished loading isn't the login page else if (url.StartsWith("javascript:") == false && url.StartsWith("about:") == false) { try { dynamic parentalNotice = htmldoc.GetElementById("parental_notice"); if (parentalNotice != null) { if (parentalNotice.OuterHtml != "") { // Steam family options enabled wbAuth.Show(); Width = 1000; Height = 350; return; } } } catch (Exception) { } // Get a list of cookies from the current page var container = GetUriCookieContainer(wbAuth.Url); var cookies = container.GetCookies(wbAuth.Url); // Go through the cookie data so that we can extract the cookies we are looking for foreach (Cookie cookie in cookies) { // Save the "sessionid" cookie if (cookie.Name == "sessionid") { Settings.Default.sessionid = cookie.Value; } // Save the "steamLogin" cookie and construct and save the user's profile link else if (cookie.Name == "steamLogin") { Settings.Default.steamLogin = cookie.Value; Settings.Default.myProfileURL = SteamProfile.GetSteamUrl(); } // Save the "steamparental" cookie" else if (cookie.Name == "steamparental") { Settings.Default.steamparental = cookie.Value; } else if (cookie.Name == "steamRememberLogin") { Settings.Default.steamRememberLogin = cookie.Value; } // Save the "steamLoginSecure" cookie" else if (cookie.Name == "steamLoginSecure") { Settings.Default.steamLoginSecure = cookie.Value; } } // Save all of the data to the program settings file, and close this form Settings.Default.Save(); Close(); } }
internal string GetCookieValue(Uri SiteUri, string name) { Cookie cookie = cookies.GetCookies(SiteUri)[name]; return((cookie == null) ? null : cookie.Value); }
/// <summary> /// 使用Post方法获取字符串结果 /// </summary> /// <param name="url"></param> /// <param name="formItems">Post表单内容</param> /// <param name="cookieContainer"></param> /// <param name="timeOut">默认20秒</param> /// <param name="encoding">响应内容的编码类型(默认utf-8)</param> /// <returns></returns> public string PostForm(string url, List <FormItem> formItems, CookieContainer cookieContainer = null, string refererUrl = null, Encoding encoding = null, int timeOut = 20000) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); #region 初始化请求对象 request.Proxy = null; request.Method = "POST"; request.Timeout = timeOut; request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; request.KeepAlive = true; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36"; if (!string.IsNullOrEmpty(refererUrl)) { request.Referer = refererUrl; } if (cookieContainer != null) { request.CookieContainer = cookieContainer; } #endregion string boundary = "----" + DateTime.Now.Ticks.ToString("x");//分隔符 request.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); //请求流 var postStream = new MemoryStream(); #region 处理Form表单请求内容 //是否用Form上传文件 var formUploadFile = formItems != null && formItems.Count > 0; if (formUploadFile) { //文件数据模板 string fileFormdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"" + "\r\nContent-Type: application/octet-stream" + "\r\n\r\n"; //文本数据模板 string dataFormdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"" + "\r\n\r\n{1}"; foreach (var item in formItems) { string formdata = null; if (item.IsFile) { //上传文件 formdata = string.Format( fileFormdataTemplate, item.Key, //表单键 item.FileName); } else { //上传文本 formdata = string.Format( dataFormdataTemplate, item.Key, item.Value); } //统一处理 byte[] formdataBytes = null; //第一行不需要换行 if (postStream.Length == 0) { formdataBytes = Encoding.UTF8.GetBytes(formdata.Substring(2, formdata.Length - 2)); } else { formdataBytes = Encoding.UTF8.GetBytes(formdata); } postStream.Write(formdataBytes, 0, formdataBytes.Length); //写入文件内容 if (item.FileContent != null && item.FileContent.Length > 0) { postStream.Write(item.FileContent, 0, item.FileContent.Length); } } //结尾 var footer = Encoding.UTF8.GetBytes("\r\n--" + boundary + "--\r\n"); postStream.Write(footer, 0, footer.Length); } else { request.ContentType = "application/x-www-form-urlencoded"; } #endregion request.ContentLength = postStream.Length; #region 输入二进制流 if (postStream != null) { postStream.Position = 0; //直接写入流 Stream requestStream = request.GetRequestStream(); byte[] buffer = new byte[1024]; int bytesRead = 0; while ((bytesRead = postStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); } postStream.Close();//关闭文件访问 } #endregion HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (cookieContainer != null) { response.Cookies = cookieContainer.GetCookies(response.ResponseUri); } using (Stream responseStream = response.GetResponseStream()) { using (StreamReader myStreamReader = new StreamReader(responseStream, encoding ?? Encoding.UTF8)) { string retString = myStreamReader.ReadToEnd(); return(retString); } } } catch (Exception exp) { Logger.AddLog(this.GetType(), "PostForm", exp.Message); return(""); } }
public bool Authenticate() { //Trace.WriteLine("Attemping to authenticate"); try { //Trace.WriteLine("R1"); HttpWebRequest request = HttpWebRequest.Create("https://www.google.com/voice") as HttpWebRequest; request.CookieContainer = jar; request.AllowAutoRedirect = true; HttpWebResponse response = request.GetResponse() as HttpWebResponse; StreamReader reader = new StreamReader(response.GetResponseStream()); string ret = reader.ReadToEnd(); //Trace.WriteLine("-> " + response.ResponseUri.ToString()); string gc = response.ResponseUri.ToString(); int x = gc.IndexOf("gsessionid"); if (x > -1) { gc = gc.Substring(x + 11); } else { Trace.WriteLine("No GSessionID"); gc = ""; } GSessionID = gc; // Trace.WriteLine("Session: " + gc); //Trace.WriteLine("R2"); request = HttpWebRequest.Create("https://www.google.com/accounts/ServiceLogin?passive=true&service=grandcentral<mpl=bluebar&continue=https%3A%2F%2Fwww.google.com%2Fvoice%2Faccount%2Fsignin%2F%3Fprev%3D%252F&gsessionid=" + gc) as HttpWebRequest; request.AllowAutoRedirect = true; request.CookieContainer = jar; response = request.GetResponse() as HttpWebResponse; reader = new StreamReader(response.GetResponseStream()); ret = reader.ReadToEnd(); //Trace.WriteLine("-> " + response.ResponseUri.ToString()); //Trace.WriteLine("Cookies: " + jar.Count); //string r = wc.DownloadString("https://www.google.com/voice"); //Trace.WriteLine("Sending Authentication..."); //https://www.google.com/accounts/ServiceLoginAuth?service=grandcentral CookieCollection cc = jar.GetCookies(new Uri("https://www.google.com/accounts")); string galx = ""; foreach (Cookie c in cc) { Trace.WriteLine(c.Name + ": " + c.Value); if (c.Name.ToUpper() == "GALX") { galx = c.Value; } } if (galx == "") { Trace.WriteLine("GALX was not found!"); } string p = "ltmpl=bluebar&continue=https%3A%2F%2Fwww.google.com%2Fvoice%2Faccount%2Fsignin%2F%3Fprev%3D%252F&service=grandcentral<mpl=bluebar<mpl=bluebar&GALX=" + galx + "&Email=" + System.Web.HttpUtility.UrlEncode(UserName) + "&Passwd=" + System.Web.HttpUtility.UrlEncode(Password) + "&rmShown=1&signIn=Sign+in&asts="; byte[] data = Encoding.ASCII.GetBytes(p); //Trace.WriteLine("R3"); request = HttpWebRequest.Create("https://www.google.com/accounts/ServiceLoginAuth?service=grandcentral") as HttpWebRequest; request.Method = "POST"; request.AllowAutoRedirect = true; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = p.Length; request.Referer = response.ResponseUri.ToString(); request.ContentLength = p.Length; request.CookieContainer = jar; Stream newStream = request.GetRequestStream(); // Send the data. newStream.Write(data, 0, data.Length); newStream.Close(); response = request.GetResponse() as HttpWebResponse; reader = new StreamReader(response.GetResponseStream()); ret = reader.ReadToEnd(); //File.WriteAllText("f:\\response.html", ret); int s = ret.IndexOf("_rnr_se"); if (s > -1) { s = ret.IndexOf("value=", s); if (s > -1) { s += 7; RNR_SE = ret.Substring(s, ret.IndexOf("\"", s) - s); } } else { Trace.WriteLine("RNR_SE is missing!"); return(false); } //Trace.WriteLine("rnr_se: " + RNR_SE); //Trace.WriteLine("-> " + response.ResponseUri.ToString()); //Trace.WriteLine("Cookies: " + jar.Count); //GetPhones(); StartMonitor(); Authenticated = true; return(true); } catch (Exception ex) { Trace.WriteLine("Couldn't authenticate: " + ex); return(false); } }
// 模拟POST访问 public static string sendPost(string postUrl, string postDataStr) { System.Net.ServicePointManager.DefaultConnectionLimit = 100; System.Net.ServicePointManager.Expect100Continue = false; //用来存放cookie CookieContainer cookie = null; HttpWebRequest request = null; Stream myRequestStream = null; HttpWebResponse response = null; Stream myResponseStream = null; StreamReader myStreamReader = null; try { //转化 byte[] byteArray = Encoding.UTF8.GetBytes(postDataStr); cookie = new CookieContainer(); //发送一个POST请求 request = (HttpWebRequest)WebRequest.Create(new Uri(postUrl)); request.CookieContainer = cookie; request.Timeout = 3000; request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = byteArray.Length; myRequestStream = request.GetRequestStream(); myRequestStream.Write(byteArray, 0, byteArray.Length); myRequestStream.Close(); //获取返回的内容 response = (HttpWebResponse)request.GetResponse(); response.Cookies = cookie.GetCookies(response.ResponseUri); myResponseStream = response.GetResponseStream(); myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); return(myStreamReader.ReadToEnd()); } catch (Exception ex) { Console.WriteLine("postUrl = " + postUrl + " Exception" + ex); } finally { if (myStreamReader != null) { myStreamReader.Close(); } if (myResponseStream != null) { myResponseStream.Close(); } if (response != null) { response.Close(); } if (request != null) { request.Abort(); } } return(""); }
//http://www.itokit.com/2012/0721/74607.html public static string HttpPost(string url, string Referer, string data, Encoding encode, bool SaveCookie, int timeout = 100000) { string dat = ""; HttpWebRequest req; if (AmountOfRunningPosting == 0) { System.GC.Collect(); } AmountOfRunningPosting++; try { req = WebRequest.Create(url) as HttpWebRequest; req.CookieContainer = cookies; req.ContentType = "application/x-www-form-urlencoded"; req.Method = "POST"; req.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:30.0) Gecko/20100101 Firefox/30.0"; req.Proxy = null; req.Timeout = timeout; req.ProtocolVersion = HttpVersion.Version10; if (!string.IsNullOrEmpty(Referer)) { req.Referer = Referer; } byte[] mybyte = Encoding.Default.GetBytes(data); req.ContentLength = mybyte.Length; Stream stream = req.GetRequestStream(); stream.Write(mybyte, 0, mybyte.Length); HttpWebResponse res = req.GetResponse() as HttpWebResponse; stream.Close(); if (SaveCookie) { CookieCollection = res.Cookies; cookies.GetCookies(req.RequestUri); } StreamReader SR = new StreamReader(res.GetResponseStream(), encode); dat = SR.ReadToEnd(); res.Close(); req.Abort(); } catch (HttpException) { AmountOfRunningPosting--; return(""); } catch (WebException) { AmountOfRunningPosting--; return(""); } if (Program.formlogin != null) { Program.formlogin.textBoxLog.Text = dat; if (!dat.Equals("")) { Program.formlogin.listBoxLog.Items.Insert(0, dat); } } AmountOfRunningPosting--; return(dat); }
public CookieCollection GetCookie(string uri) { return(container_.GetCookies(new Uri(uri))); }
public static async Task <GetWebPageResponse> GetWebPage(string Url) { var webPageResponse = new GetWebPageResponse(); bool urlCorrect = Uri.TryCreate(Url, UriKind.Absolute, out Uri uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); if (!urlCorrect) { Debug.Write("GetWeb." + System.Reflection.MethodBase.GetCurrentMethod().Name + " => URL not correct"); return(webPageResponse); } using (var handler = new HttpClientHandler()) { var cookies = new CookieContainer(); handler.CookieContainer = cookies; using (HttpClient client = new HttpClient(handler) { Timeout = TimeSpan.FromSeconds(6) }) { try { using (HttpResponseMessage response = await client.GetAsync(Url).ConfigureAwait(false)) { webPageResponse.ResponseCode = (short)response.StatusCode; if (!response.IsSuccessStatusCode) { Debug.Write("GetAdvertListAsync(AdvertSearch searchParams) => !response.IsSuccessStatusCode"); webPageResponse.ErrorMessage = "Niepoprawna odpowiedź z serwera"; return(webPageResponse); } Uri uri = new Uri(Url); var responseCookies = cookies.GetCookies(uri); foreach (Cookie cookie in responseCookies) { webPageResponse.CookieList.Add(cookie); } using (HttpContent content = response.Content) { var byteArray = await content.ReadAsByteArrayAsync(); Encoding iso = Encoding.GetEncoding("ISO-8859-2"); Encoding utf8 = Encoding.UTF8; byte[] utf8Bytes = Encoding.Convert(iso, utf8, byteArray); webPageResponse.BodyString.Append(System.Net.WebUtility.HtmlDecode(utf8.GetString(utf8Bytes))); } } } catch (System.Threading.Tasks.TaskCanceledException) { Debug.Write("GetAdvertListAsync(AdvertSearch searchParams) => System.Threading.Tasks.TaskCanceledException"); webPageResponse.ErrorMessage = "Błąd połączenia. Przekroczono limit połączenia"; return(webPageResponse); } catch (Exception e) { Debug.Write("GetAdvertListAsync(AdvertSearch searchParams) => " + e.Message); webPageResponse.ErrorMessage = "Błąd połączenia z serwerem."; return(webPageResponse); } } } webPageResponse.Success = true; return(webPageResponse); }
protected override async Task <WebClientByteResult> Run(WebRequest webRequest) { HttpResponseMessage response = null; var request = new HttpRequestMessage(); request.Headers.ExpectContinue = false; request.RequestUri = new Uri(webRequest.Url); //if (webRequest.EmulateBrowser == true) // request.Headers.UserAgent.ParseAdd(BrowserUtil.ChromeUserAgent); //else // request.Headers.UserAgent.ParseAdd("Jackett/" + configService.GetVersion()); // clear cookies from cookiecontainer var oldCookies = cookies.GetCookies(request.RequestUri); foreach (Cookie oldCookie in oldCookies) { oldCookie.Expired = true; } // add cookies to cookiecontainer if (!string.IsNullOrWhiteSpace(webRequest.Cookies)) { // don't include the path, Scheme is needed for mono compatibility var cookieUrl = new Uri(request.RequestUri.Scheme + "://" + request.RequestUri.Host); var cookieDictionary = CookieUtil.CookieHeaderToDictionary(webRequest.Cookies); foreach (var kv in cookieDictionary) { cookies.Add(cookieUrl, new Cookie(kv.Key, kv.Value)); } } if (webRequest.Headers != null) { foreach (var header in webRequest.Headers) { if (header.Key != "Content-Type") { request.Headers.TryAddWithoutValidation(header.Key, header.Value); } } } if (!string.IsNullOrEmpty(webRequest.Referer)) { request.Headers.Referrer = new Uri(webRequest.Referer); } if (!string.IsNullOrEmpty(webRequest.RawBody)) { var type = webRequest.Headers.Where(h => h.Key == "Content-Type").Cast <KeyValuePair <string, string>?>().FirstOrDefault(); if (type.HasValue) { var str = new StringContent(webRequest.RawBody); str.Headers.Remove("Content-Type"); str.Headers.Add("Content-Type", type.Value.Value); request.Content = str; } else { request.Content = new StringContent(webRequest.RawBody); } request.Method = HttpMethod.Post; } else if (webRequest.Type == RequestType.POST) { if (webRequest.PostData != null) { request.Content = FormUrlEncodedContentWithEncoding(webRequest.PostData, webRequest.Encoding); } request.Method = HttpMethod.Post; } else { request.Method = HttpMethod.Get; } response = await client.SendAsync(request); var result = new WebClientByteResult { Content = await response.Content.ReadAsByteArrayAsync() }; foreach (var header in response.Headers) { var value = header.Value; result.Headers[header.Key.ToLowerInvariant()] = value.ToArray(); } // some cloudflare clients are using a refresh header // Pull it out manually if (response.StatusCode == System.Net.HttpStatusCode.ServiceUnavailable && response.Headers.Contains("Refresh")) { var refreshHeaders = response.Headers.GetValues("Refresh"); var redirval = ""; var redirtime = 0; if (refreshHeaders != null) { foreach (var value in refreshHeaders) { var start = value.IndexOf("="); var end = value.IndexOf(";"); var len = value.Length; if (start > -1) { redirval = value.Substring(start + 1); result.RedirectingTo = redirval; // normally we don't want a serviceunavailable (503) to be a redirect, but that's the nature // of this cloudflare approach..don't want to alter BaseWebResult.IsRedirect because normally // it shoudln't include service unavailable..only if we have this redirect header. response.StatusCode = System.Net.HttpStatusCode.Redirect; redirtime = int.Parse(value.Substring(0, end)); System.Threading.Thread.Sleep(redirtime * 1000); } } } } if (response.Headers.Location != null) { result.RedirectingTo = response.Headers.Location.ToString(); } // Mono won't add the baseurl to relative redirects. // e.g. a "Location: /index.php" header will result in the Uri "file:///index.php" // See issue #1200 if (result.RedirectingTo != null && result.RedirectingTo.StartsWith("file://")) { // URL decoding apparently is needed to, without it e.g. Demonoid download is broken // TODO: is it always needed (not just for relative redirects)? var newRedirectingTo = WebUtilityHelpers.UrlDecode(result.RedirectingTo, webRequest.Encoding); if (newRedirectingTo.StartsWith("file:////")) // Location without protocol but with host (only add scheme) { newRedirectingTo = newRedirectingTo.Replace("file://", request.RequestUri.Scheme + ":"); } else { newRedirectingTo = newRedirectingTo.Replace("file://", request.RequestUri.Scheme + "://" + request.RequestUri.Host); } logger.Debug("[MONO relative redirect bug] Rewriting relative redirect URL from " + result.RedirectingTo + " to " + newRedirectingTo); result.RedirectingTo = newRedirectingTo; } result.Status = response.StatusCode; // Compatiblity issue between the cookie format and httpclient // Pull it out manually ignoring the expiry date then set it manually // http://stackoverflow.com/questions/14681144/httpclient-not-storing-cookies-in-cookiecontainer var responseCookies = new List <Tuple <string, string> >(); if (response.Headers.TryGetValues("set-cookie", out var cookieHeaders)) { foreach (var value in cookieHeaders) { logger.Debug(value); var nameSplit = value.IndexOf('='); if (nameSplit > -1) { responseCookies.Add(new Tuple <string, string>(value.Substring(0, nameSplit), value.Substring(0, value.IndexOf(';') == -1 ? value.Length : (value.IndexOf(';'))) + ";")); } } var cookieBuilder = new StringBuilder(); foreach (var cookieGroup in responseCookies.GroupBy(c => c.Item1)) { cookieBuilder.AppendFormat("{0} ", cookieGroup.Last().Item2); } result.Cookies = cookieBuilder.ToString().Trim(); } ServerUtil.ResureRedirectIsFullyQualified(webRequest, result); return(result); }
/// <summary> /// 二次验证 /// </summary> /// <param name="strCookieSessionID"></param> /// <param name="strCookieUserName"></param> /// <param name="strCookiePassword"></param> /// <param name="strShowBox"></param> public void LoginSec(string strCookieSessionID, string strCookieUserName, string strCookiePassword, out string strShowBox) { try { //定义Cookie容器 CookieContainer CookieArray = new CookieContainer(); //创建Http请求 HttpWebRequest LoginHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://club.qingdaonews.com/login_club_new1.php"); //登录数据 //string LoginData = "id=" + strUserName + "&passwd=" + strPassword + "&usertype=0"; //数据被传输类型 //LoginHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; //数据长度 //LoginHttpWebRequest.ContentLength = LoginData.Length; //数据传输方法 get或post LoginHttpWebRequest.Method = "POST"; LoginHttpWebRequest.Accept = "*/*"; LoginHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; MAXTHON 2.0)"; //设置HttpWebRequest的CookieContainer为刚才建立的那个CookieArray LoginHttpWebRequest.CookieContainer = CookieArray; CookieArray.Add(new Uri("http://club.qingdaonews.com"), new Cookie("PHPSESSID", strCookieSessionID)); CookieArray.Add(new Uri("http://club.qingdaonews.com"), new Cookie("qingdaonews[username]", strCookieUserName)); CookieArray.Add(new Uri("http://club.qingdaonews.com"), new Cookie("qingdaonews[password]", strCookiePassword)); //获取登录数据流 Stream myRequestStream = LoginHttpWebRequest.GetRequestStream(); //StreamWriter StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")); //把数据写入HttpWebRequest的Request流 //myStreamWriter.Write(LoginData); //关闭打开对象 //myStreamWriter.Close(); myRequestStream.Close(); //新建一个HttpWebResponse HttpWebResponse myHttpWebResponse = (HttpWebResponse)LoginHttpWebRequest.GetResponse(); //获取一个包含url的Cookie集合的CookieCollection myHttpWebResponse.Cookies = CookieArray.GetCookies(LoginHttpWebRequest.RequestUri); if (myHttpWebResponse.Cookies.Count > 0) { CookieArray.Add(myHttpWebResponse.Cookies); } WebHeaderCollection a = myHttpWebResponse.Headers; Stream myResponseStream = myHttpWebResponse.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312")); strShowBox = myStreamReader.ReadToEnd(); //把数据从HttpWebResponse的Response流中读出 myStreamReader.Close(); myResponseStream.Close(); string[] arrClubList = { "club_entry_2_2_0_1_0.htm", "club_entry_1025_2_0_1_0.htm", "club_entry_48_2_0_1_0.htm", "club_entry_67_2_0_1_0.htm", "club_entry_57_2_0_1_0.htm", "club_entry_9_2_0_1_0.htm", "club_entry_1038_2_0_1_0.htm", "club_entry_88_2_0_1_0.htm", "club_entry_123_2_0_1_0.htm", "club_entry_1_2_0_1_0.htm", "club_entry_156_2_0_1_0.htm", "club_entry_1018_2_0_1_0.htm", "club_entry_1023_2_0_1_0.htm", "club_entry_1024_2_0_1_0.htm", "club_entry_1115_2_0_1_0.htm", "club_entry_1133_2_0_1_0.htm", "club_entry_41_2_0_1_0.htm", "club_entry_1030_2_0_1_0.htm", "club_entry_1170_2_0_1_0.htm", "club_entry_1171_2_0_1_0.htm", "club_entry_1181_2_0_1_0.htm", "club_entry_1188_2_0_1_0.htm", "club_entry_1192_2_0_1_0.htm", "club_entry_1199_2_0_1_0.htm", "club_entry_128_3_0_1_0.htm", "club_entry_129_3_0_1_0.htm", "club_entry_130_3_0_1_0.htm", "club_entry_131_3_0_1_0.htm", "club_entry_132_3_0_1_0.htm", "club_entry_133_3_0_1_0.htm", "club_entry_134_3_0_1_0.htm", "club_entry_173_3_0_1_0.htm", "club_entry_1053_3_0_1_0.htm", "club_entry_1054_3_0_1_0.htm", "club_entry_1059_3_0_1_0.htm", "club_entry_1060_3_0_1_0.htm", "club_entry_39_4_0_1_0.htm", "club_entry_1039_4_0_1_0.htm", "club_entry_1040_4_0_1_0.htm", "club_entry_1041_4_0_1_0.htm", "club_entry_1042_4_0_1_0.htm", "club_entry_1043_4_0_1_0.htm", "club_entry_1044_4_0_1_0.htm", "club_entry_1045_4_0_1_0.htm", "club_entry_1046_4_0_1_0.htm", "club_entry_1047_4_0_1_0.htm", "club_entry_1048_4_0_1_0.htm", "club_entry_1049_4_0_1_0.htm", "club_entry_1052_4_0_1_0.htm", "club_entry_1055_4_0_1_0.htm", "club_entry_1056_4_0_1_0.htm", "club_entry_1057_4_0_1_0.htm", "club_entry_1061_4_0_1_0.htm", "club_entry_1062_4_0_1_0.htm", "club_entry_1063_4_0_1_0.htm", "club_entry_1069_4_0_1_0.htm", "club_entry_1070_4_0_1_0.htm", "club_entry_1071_4_0_1_0.htm", "club_entry_154_4_0_1_0.htm", "club_entry_1085_4_0_1_0.htm", "club_entry_1086_4_0_1_0.htm", "club_entry_1179_4_0_1_0.htm", "club_entry_1102_4_0_1_0.htm", "club_entry_1108_4_0_1_0.htm", "club_entry_1109_4_0_1_0.htm", "club_entry_1111_4_0_1_0.htm", "club_entry_1121_4_0_1_0.htm", "club_entry_1124_4_0_1_0.htm", "club_entry_1130_4_0_1_0.htm", "club_entry_1131_4_0_1_0.htm", "club_entry_1143_4_0_1_0.htm", "club_entry_1157_4_0_1_0.htm", "club_entry_1158_4_0_1_0.htm", "club_entry_1159_4_0_1_0.htm", "club_entry_1169_4_0_1_0.htm", "club_entry_1173_4_0_1_0.htm", "club_entry_1184_4_0_1_0.htm", "club_entry_1185_4_0_1_0.htm", "club_entry_1190_4_0_1_0.htm", "club_entry_1172_4_0_1_0.htm", "club_entry_20_5_0_1_0.htm", "club_entry_1010_5_0_1_0.htm", "club_entry_49_5_0_1_0.htm", "club_entry_1139_5_0_1_0.htm", "club_entry_1115_6_0_1_0.htm", "club_entry_47_6_0_1_0.htm", "club_entry_66_6_0_1_0.htm", "club_entry_12_6_0_1_0.htm", "club_entry_86_6_0_1_0.htm", "club_entry_163_6_0_1_0.htm", "club_entry_13_6_0_1_0.htm", "club_entry_94_6_0_1_0.htm", "club_entry_83_6_0_1_0.htm", "club_entry_174_6_0_1_0.htm", "club_entry_175_6_0_1_0.htm", "club_entry_176_6_0_1_0.htm", "club_entry_177_6_0_1_0.htm", "club_entry_1006_6_0_1_0.htm", "club_entry_1007_6_0_1_0.htm", "club_entry_1011_6_0_1_0.htm", "club_entry_1031_6_0_1_0.htm", "club_entry_58_7_0_1_0.htm", "club_entry_1005_7_0_1_0.htm", "club_entry_160_7_0_1_0.htm", "club_entry_121_7_0_1_0.htm", "club_entry_1099_7_0_1_0.htm", "club_entry_1100_7_0_1_0.htm", "club_entry_64_8_0_1_0.htm", "club_entry_1064_8_0_1_0.htm", "club_entry_1094_8_0_1_0.htm", "club_entry_1127_8_0_1_0.htm", "club_entry_1128_8_0_1_0.htm", "club_entry_1142_8_0_1_0.htm", "club_entry_1201_8_0_1_0.htm", "club_entry_4_9_0_1_0.htm", "club_entry_5_9_0_1_0.htm", "club_entry_36_9_0_1_0.htm", "club_entry_52_9_0_1_0.htm", "club_entry_74_9_0_1_0.htm", "club_entry_76_9_0_1_0.htm", "club_entry_92_9_0_1_0.htm", "club_entry_124_9_0_1_0.htm", "club_entry_125_9_0_1_0.htm", "club_entry_146_9_0_1_0.htm", "club_entry_162_9_0_1_0.htm", "club_entry_1015_9_0_1_0.htm", "club_entry_1016_9_0_1_0.htm", "club_entry_1033_9_0_1_0.htm", "club_entry_1068_9_0_1_0.htm", "club_entry_1084_9_0_1_0.htm", "club_entry_1125_9_0_1_0.htm", "club_entry_1151_9_0_1_0.htm", "club_entry_33_10_0_1_0.htm", "club_entry_93_10_0_1_0.htm", "club_entry_77_10_0_1_0.htm", "club_entry_1072_10_0_1_0.htm","club_entry_1073_10_0_1_0.htm", "club_entry_1027_10_0_1_0.htm", "club_entry_1066_10_0_1_0.htm", "club_entry_1077_10_0_1_0.htm", "club_entry_1101_10_0_1_0.htm", "club_entry_1117_10_0_1_0.htm","club_entry_1189_10_0_1_0.htm", "club_entry_40_11_0_1_0.htm", "club_entry_1026_11_0_1_0.htm", "club_entry_24_11_0_1_0.htm", "club_entry_43_11_0_1_0.htm", "club_entry_54_11_0_1_0.htm", "club_entry_70_11_0_1_0.htm", "club_entry_72_11_0_1_0.htm", "club_entry_81_11_0_1_0.htm", "club_entry_84_11_0_1_0.htm", "club_entry_122_11_0_1_0.htm", "club_entry_108_11_0_1_0.htm", "club_entry_22_11_0_1_0.htm", "club_entry_61_11_0_1_0.htm", "club_entry_8_11_0_1_0.htm", "club_entry_42_11_0_1_0.htm", "club_entry_1126_11_0_1_0.htm", "club_entry_1129_11_0_1_0.htm","club_entry_1156_11_0_1_0.htm", "club_entry_145_11_0_1_0.htm", "club_entry_107_11_0_1_0.htm", "club_entry_144_11_0_1_0.htm", "club_entry_1191_11_0_1_0.htm", "club_entry_1144_11_0_1_0.htm","club_entry_7_12_0_1_0.htm", "club_entry_27_12_0_1_0.htm", "club_entry_1193_12_0_1_0.htm", "club_entry_1183_12_0_1_0.htm", "club_entry_113_12_0_1_0.htm", "club_entry_46_12_0_1_0.htm", "club_entry_60_12_0_1_0.htm", "club_entry_1187_12_0_1_0.htm", "club_entry_82_12_0_1_0.htm", "club_entry_98_12_0_1_0.htm", "club_entry_1168_12_0_1_0.htm", "club_entry_1180_12_0_1_0.htm","club_entry_1075_12_0_1_0.htm", "club_entry_1078_12_0_1_0.htm", "club_entry_1087_12_0_1_0.htm", "club_entry_38_12_0_1_0.htm", "club_entry_141_12_0_1_0.htm", "club_entry_96_12_0_1_0.htm", "club_entry_97_12_0_1_0.htm", "club_entry_109_12_0_1_0.htm", "club_entry_1032_12_0_1_0.htm", "club_entry_147_12_0_1_0.htm", "club_entry_1013_12_0_1_0.htm", "club_entry_1149_12_0_1_0.htm","club_entry_149_12_0_1_0.htm", "club_entry_1165_12_0_1_0.htm", "club_entry_1200_12_0_1_0.htm", "club_entry_26_17_0_1_0.htm", "club_entry_1067_17_0_1_0.htm", "club_entry_120_17_0_1_0.htm", "club_entry_1096_17_0_1_0.htm", "club_entry_1020_17_0_1_0.htm", "club_entry_1145_17_0_1_0.htm", "club_entry_1160_17_0_1_0.htm", "club_entry_71_13_0_1_0.htm", "club_entry_166_13_0_1_0.htm", "club_entry_167_13_0_1_0.htm", "club_entry_168_13_0_1_0.htm", "club_entry_169_13_0_1_0.htm", "club_entry_170_13_0_1_0.htm", "club_entry_165_13_0_1_0.htm", "club_entry_172_13_0_1_0.htm", "club_entry_1018_13_0_1_0.htm", "club_entry_1030_13_0_1_0.htm", "club_entry_1076_13_0_1_0.htm", "club_entry_1082_13_0_1_0.htm", "club_entry_47_14_0_1_0.htm", "club_entry_29_14_0_1_0.htm", "club_entry_37_14_0_1_0.htm", "club_entry_1080_14_0_1_0.htm", "club_entry_1123_14_0_1_0.htm", "club_entry_93_14_0_1_0.htm", "club_entry_112_14_0_1_0.htm", "club_entry_118_14_0_1_0.htm", "club_entry_119_14_0_1_0.htm", "club_entry_126_14_0_1_0.htm", "club_entry_159_14_0_1_0.htm", "club_entry_164_14_0_1_0.htm", "club_entry_69_14_0_1_0.htm", "club_entry_1021_14_0_1_0.htm","club_entry_1089_14_0_1_0.htm", "club_entry_1091_14_0_1_0.htm", "club_entry_1116_14_0_1_0.htm", "club_entry_1122_14_0_1_0.htm", "club_entry_3_14_0_1_0.htm", "club_entry_111_14_0_1_0.htm", "club_entry_1012_14_0_1_0.htm", "club_entry_1029_14_0_1_0.htm", "club_entry_73_14_0_1_0.htm", "club_entry_1092_14_0_1_0.htm", "club_entry_1137_14_0_1_0.htm", "club_entry_1090_14_0_1_0.htm","club_entry_1019_14_0_1_0.htm", "club_entry_1050_14_0_1_0.htm", "club_entry_114_14_0_1_0.htm", "club_entry_1034_14_0_1_0.htm", "club_entry_1110_14_0_1_0.htm", "club_entry_1134_14_0_1_0.htm","club_entry_143_14_0_1_0.htm", "club_entry_1135_14_0_1_0.htm", "club_entry_152_14_0_1_0.htm", "club_entry_1164_14_0_1_0.htm", "club_entry_1174_14_0_1_0.htm", "club_entry_1175_14_0_1_0.htm","club_entry_1020_14_0_1_0.htm", "club_entry_1182_14_0_1_0.htm", "club_entry_1194_14_0_1_0.htm", "club_entry_1197_14_0_1_0.htm", "club_entry_1198_14_0_1_0.htm", "club_entry_44_15_0_1_0.htm", "club_entry_2_15_0_1_0.htm", "club_entry_135_15_0_1_0.htm", "club_entry_99_15_0_1_0.htm", "club_entry_91_15_0_1_0.htm", "club_entry_100_15_0_1_0.htm", "club_entry_101_15_0_1_0.htm", "club_entry_102_15_0_1_0.htm", "club_entry_103_15_0_1_0.htm", "club_entry_105_15_0_1_0.htm", "club_entry_106_15_0_1_0.htm", "club_entry_110_15_0_1_0.htm", "club_entry_115_15_0_1_0.htm", "club_entry_136_15_0_1_0.htm", "club_entry_137_15_0_1_0.htm", "club_entry_138_15_0_1_0.htm", "club_entry_139_15_0_1_0.htm", "club_entry_140_15_0_1_0.htm", "club_entry_30_16_0_1_0.htm", "club_entry_68_16_0_1_0.htm", "club_entry_87_16_0_1_0.htm", "club_entry_1093_16_0_1_0.htm" }; string strOut; string strURL = "http://club.qingdaonews.com/club_entry_39_2_0_1_0.htm"; ShowClubList(strURL, strCookieSessionID, strCookieUserName, strCookiePassword, out strOut); //MessageBox.Show(strOut); } catch (Exception ex) { throw ex; } }
void IHttpHandler.ProcessRequest(HttpContext Context) { string ServerURL = ""; string ext = ".tunnel"; string protocol = "http://"; try { // Parsing incoming URL and extracting original server URL char[] URL_Separator = { '/' }; string[] URL_List = Context.Request.Url.AbsoluteUri.Remove(0, protocol.Length).Split(URL_Separator); ServerURL = protocol + URL_List[2].Remove(URL_List[2].Length - ext.Length, ext.Length) + @"/"; string URLPrefix = @"/" + URL_List[1] + @"/" + URL_List[2]; // Eg. "/handler/stg2web.tunnel"; for ( int i = 3; i < URL_List.Length; i++ ) { ServerURL += URL_List[i] + @"/"; } ServerURL = ServerURL.Remove(ServerURL.Length -1, 1); WriteLog(ServerURL + " (" + Context.Request.Url.ToString() + ")"); // Extracting POST data from incoming request Stream RequestStream = Context.Request.InputStream; byte[] PostData = new byte[Context.Request.InputStream.Length]; RequestStream.Read(PostData, 0, (int) Context.Request.InputStream.Length); // Creating proxy web request HttpWebRequest ProxyRequest = (HttpWebRequest) WebRequest.Create(ServerURL); if (false) { ProxyRequest.Proxy = new WebProxy("proxy1:80", true); } ProxyRequest.Method = Context.Request.HttpMethod; ProxyRequest.UserAgent = Context.Request.UserAgent; CookieContainer ProxyCookieContainer = new CookieContainer(); ProxyRequest.CookieContainer = new CookieContainer(); ProxyRequest.CookieContainer.Add(ProxyCookieContainer.GetCookies(new Uri(ServerURL))); ProxyRequest.KeepAlive = true; // For POST, write the post data extracted from the incoming request if (ProxyRequest.Method == "POST") { ProxyRequest.ContentType = "application/x-www-form-urlencoded"; ProxyRequest.ContentLength = PostData.Length; Stream ProxyRequestStream = ProxyRequest.GetRequestStream(); ProxyRequestStream.Write(PostData, 0, PostData.Length); ProxyRequestStream.Close(); } // Getting response from the proxy request HttpWebResponse ProxyResponse = (HttpWebResponse) ProxyRequest.GetResponse(); if (ProxyRequest.HaveResponse) { // Handle cookies foreach(Cookie ReturnCookie in ProxyResponse.Cookies) { bool CookieFound = false; foreach(Cookie OldCookie in ProxyCookieContainer.GetCookies(new Uri(ServerURL))) { if (ReturnCookie.Name.Equals(OldCookie.Name)) { OldCookie.Value = ReturnCookie.Value; CookieFound = true; } } if (!CookieFound) { ProxyCookieContainer.Add(ReturnCookie); } } } Stream StreamResponse = ProxyResponse.GetResponseStream(); int ResponseReadBufferSize = 256; byte[] ResponseReadBuffer = new byte[ResponseReadBufferSize]; MemoryStream MemoryStreamResponse = new MemoryStream(); int ResponseCount = StreamResponse.Read(ResponseReadBuffer, 0, ResponseReadBufferSize); while ( ResponseCount > 0 ) { MemoryStreamResponse.Write(ResponseReadBuffer, 0, ResponseCount); ResponseCount = StreamResponse.Read(ResponseReadBuffer, 0, ResponseReadBufferSize); } byte[] ResponseData = MemoryStreamResponse.ToArray(); string ResponseDataString = Encoding.ASCII.GetString(ResponseData); Context.Response.ContentType = ProxyResponse.ContentType; // While rendering HTML, parse and modify the URLs present if (ProxyResponse.ContentType.StartsWith("text/html")) { HTML.ParseHTML Parser = new HTML.ParseHTML(); Parser.Source = ResponseDataString; while (!Parser.Eof()) { char ch = Parser.Parse(); if (ch == 0) { HTML.AttributeList Tag = Parser.GetTag(); if (Tag.Name.Equals("img", StringComparison.InvariantCultureIgnoreCase)) { // Adjust image tags } if (Tag["href"] != null) { if (Tag["href"].Value.StartsWith(@"/")) { WriteLog("URL " + Tag["href"].Value + " modified to " + URLPrefix + Tag["href"].Value); ResponseDataString = ResponseDataString.Replace( "\"" + Tag["href"].Value + "\"", "\"" + URLPrefix + Tag["href"].Value + "\""); } } if (Tag["src"] != null) { if (Tag["src"].Value.StartsWith(@"/")) { WriteLog("URL " + Tag["src"].Value + " modified to " + URLPrefix + Tag["src"].Value); ResponseDataString = ResponseDataString.Replace( "\"" + Tag["src"].Value + "\"", "\"" + URLPrefix + Tag["src"].Value + "\""); } } } } // Replace script/style file url's ResponseDataString = ResponseDataString.Replace( "url('/", "url('" + URLPrefix + "/"); // replace flash file url's ResponseDataString = ResponseDataString.Replace( "swf: '/", "swf: '" + URLPrefix + "/"); Context.Response.Write(ResponseDataString); } else { Context.Response.OutputStream.Write(ResponseData, 0, ResponseData.Length); } MemoryStreamResponse.Close(); StreamResponse.Close(); ProxyResponse.Close(); } catch (Exception Ex) { Context.Response.Write(Ex.Message.ToString()); WriteLog("An error has occurred while requesting the URL " + ServerURL + "(" + Context.Request.Url.ToString() + ")\n" + Ex.ToString()); } }
/// <summary> /// 检索论坛信息 /// </summary> /// <param name="strURL"></param> /// <param name="strCookieSessionID"></param> /// <param name="strCookieUserName"></param> /// <param name="strCookiePassword"></param> /// <param name="strShowBox"></param> public void ShowClubList(string strURL, string strCookieSessionID, string strCookieUserName, string strCookiePassword, out string strShowBox) { toolStripStatusLabel1.Text = "正在请求:" + strURL; try { //定义Cookie容器 CookieContainer CookieArray = new CookieContainer(); //创建Http请求 HttpWebRequest LoginHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL); //登录数据 //string LoginData = "id=" + strUserName + "&passwd=" + strPassword + "&usertype=0"; //数据被传输类型 //LoginHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; //数据长度 //LoginHttpWebRequest.ContentLength = LoginData.Length; //数据传输方法 get或post LoginHttpWebRequest.Method = "POST"; LoginHttpWebRequest.Accept = "*/*"; LoginHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; MAXTHON 2.0)"; //设置HttpWebRequest的CookieContainer为刚才建立的那个CookieArray LoginHttpWebRequest.CookieContainer = CookieArray; CookieArray.Add(new Uri("http://club.qingdaonews.com"), new Cookie("PHPSESSID", strCookieSessionID)); CookieArray.Add(new Uri("http://club.qingdaonews.com"), new Cookie("qingdaonews[username]", strCookieUserName)); CookieArray.Add(new Uri("http://club.qingdaonews.com"), new Cookie("qingdaonews[password]", strCookiePassword)); CookieArray.Add(new Uri("http://club.qingdaonews.com"), new Cookie("qingdaonews", "")); //获取登录数据流 Stream myRequestStream = LoginHttpWebRequest.GetRequestStream(); //StreamWriter StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")); //把数据写入HttpWebRequest的Request流 //myStreamWriter.Write(LoginData); //关闭打开对象 //myStreamWriter.Close(); myRequestStream.Close(); //新建一个HttpWebResponse HttpWebResponse myHttpWebResponse = (HttpWebResponse)LoginHttpWebRequest.GetResponse(); //获取一个包含url的Cookie集合的CookieCollection myHttpWebResponse.Cookies = CookieArray.GetCookies(LoginHttpWebRequest.RequestUri); if (myHttpWebResponse.Cookies.Count > 0) { CookieArray.Add(myHttpWebResponse.Cookies); } string strList_display_time = myHttpWebResponse.Cookies[0].Value.ToString(); WebHeaderCollection a = myHttpWebResponse.Headers; Stream myResponseStream = myHttpWebResponse.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312")); strShowBox = myStreamReader.ReadToEnd(); //把数据从HttpWebResponse的Response流中读出 myStreamReader.Close(); myResponseStream.Close(); try { Regex regexObj = new Regex(@"<a href=""\w{12,30}\.htm"">[\u4e00-\u9fa5, ,0-9,\uFF00-\uFFFF,\w]{4,5}</a>"); Match matchResults = regexObj.Match(strShowBox); while (matchResults.Success) { for (int i = 1; i < matchResults.Groups.Count; i++) { Group groupObj = matchResults.Groups[i]; if (groupObj.Success) { //matched groupObj.Value.ToString(); // matched text: groupObj.Value // match start: groupObj.Index // match length: groupObj.Length } } matchResults = matchResults.NextMatch(); string resultString_0 = null; string resultString_1 = null; try { resultString_0 = Regex.Match(matchResults.ToString(), @"club_entry_\d{1,9}_\d{1,9}_\d{1,9}_\d{1,9}_\d{1,9}\.htm").Value; resultString_1 = Regex.Match(matchResults.ToString(), @">[\u4e00-\u9fa5,\uFF00-\uFFFF, ,0-9,\w]{1,5}<").Value; ListViewItem li = new ListViewItem(); li.SubItems.Clear(); li.SubItems[0].Text = resultString_0; li.SubItems.Add(resultString_1); listView1.Items.Add(li); } catch (ArgumentException ex) { // Syntax error in the regular expression } } } catch (ArgumentException ex) { // Syntax error in the regular expression } try { int num = 1; Regex regexObj_1 = new Regex(@"showAnnounce_\d{1,9}_\d{1,9}_1_\d{1,9}\.htm"); Match matchResults = regexObj_1.Match(strShowBox); while (matchResults.Success) { for (int i = 1; i < matchResults.Groups.Count; i++) { Group groupObj = matchResults.Groups[i]; if (groupObj.Success) { //matched groupObj.Value.ToString(); // matched text: groupObj.Value // match start: groupObj.Index // match length: groupObj.Length } } matchResults = matchResults.NextMatch(); ListViewItem li = new ListViewItem(); li.SubItems.Clear(); li.SubItems[0].Text = num.ToString(); li.SubItems.Add(matchResults.Value.ToString()); listView2.Items.Add(li); num++; } } catch (ArgumentException ex) { // Syntax error in the regular expression } int intLV1 = listView1.Items.Count; int intLV2 = listView2.Items.Count; listView1.Items[intLV1 - 1].Remove(); listView2.Items[intLV2 - 1].Remove(); toolStripStatusLabel1.Text = "正在访问:" + strURL; Random ran = new Random(); int RanKey = ran.Next(0, listView2.Items.Count); //showAnnounce_2_4576914_1_0.htm string strAnnounceID = listView2.Items[RanKey].SubItems[1].Text.ToString(); string B_1 = ""; string T_1 = ""; try { B_1 = Regex.Match(strAnnounceID, @"e_\d{1,9}").Value.ToString(); } catch (ArgumentException ex) { MessageBox.Show(ex.ToString()); } try { T_1 = Regex.Match(strAnnounceID, @"\d{1,9}_1").Value.ToString(); } catch (ArgumentException ex) { MessageBox.Show(ex.ToString()); } string strURL_1 = "http://club.qingdaonews.com/SaveReAnnounce_static.php"; string strBoard_id = B_1.Replace("e_", ""); string strTopic_id = T_1.Replace("_1", ""); string strMessages = "路过打酱油的说。"; string strOut; SendMessage(strURL_1, strAnnounceID, strBoard_id, strTopic_id, strMessages, textUserName.Text.ToString().Trim(), strCookieSessionID, strCookieUserName, strCookiePassword, strList_display_time, out strOut); } catch (Exception ex) { throw ex; } }
public static void Add_ExpiredCookie_NotAdded() { CookieContainer cc = new CookieContainer(); Cookie c1 = new Cookie("name1", "value", "", ".domain.com") { Expired = true }; Cookie c2 = new Cookie("name2", "value", "", ".domain.com"); cc.Add(c1); cc.Add(c2); // Ignores adding expired cookies Assert.Equal(1, cc.Count); Assert.Equal(c2, cc.GetCookies(new Uri("http://domain.com"))[0]); // Manually expire cookie c2.Expired = true; cc.Add(c2); Assert.Equal(0, cc.Count); }
/// <summary> /// 发贴流程 /// </summary> /// <param name="strURL"></param> /// <param name="strAnnounceID"></param> /// <param name="strBoard_id"></param> /// <param name="strTopic_id"></param> /// <param name="strMessages"></param> /// <param name="strUserName"></param> /// <param name="strCookieSessionID"></param> /// <param name="strCookieUserName"></param> /// <param name="strCookiePassword"></param> /// <param name="strList_display_time"></param> /// <param name="strShowBox"></param> public void SendMessage(string strURL, string strAnnounceID, string strBoard_id, string strTopic_id, string strMessages, string strUserName, string strCookieSessionID, string strCookieUserName, string strCookiePassword, string strList_display_time, out string strShowBox) { toolStripStatusLabel1.Text = "正在请求:http://club.qingdaonews.com/" + strAnnounceID; try { string strParent_id = strTopic_id; //定义Cookie容器 CookieContainer CookieArray = new CookieContainer(); //创建Http请求 HttpWebRequest LoginHttpWebRequest = (HttpWebRequest)WebRequest.Create(strURL); //数据格式 //viewmode= //topic_id=3807645 //parent_id=3807645 //board_id=138 //Page=1 //a_name=snoopy6973 //subject=%BB%D8%B8%B4%3A //ubb=UBB //chkSignature=1 //body=好吧,我也是路过打酱油的。 //insertimg= //登录数据 //string LoginData = "viewmode=&topic_id=" + strTopic_id + "&parent_id=" + strParent_id + "&board_id=" + strBoard_id + "&Page=1&a_name=" + strUserName + "&subject=%BB%D8%B8%B4%3A&ubb=UBB&chkSignature=1&body=" + strMessages + "&insertimg="; string LoginData = "viewmode=&topic_id=" + strTopic_id + "&parent_id=" + strParent_id + "&board_id=" + strBoard_id + "&Page=1&a_name=" + strUserName + "&subject=%BB%D8%B8%B4%3A&ubb=UBB&chkSignature=1&body=%7B201%7D&insertimg="; //数据被传输类型 LoginHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; //数据长度 LoginHttpWebRequest.ContentLength = LoginData.Length; //数据传输方法 get或post LoginHttpWebRequest.Method = "POST"; LoginHttpWebRequest.Accept = "*/*"; LoginHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; Tablet PC 2.0; MAXTHON 2.0)"; LoginHttpWebRequest.Referer = "http://club.qingdaonews.com/" + strAnnounceID; //LoginHttpWebRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 1.1.4322)"; //设置HttpWebRequest的CookieContainer为刚才建立的那个CookieArray LoginHttpWebRequest.CookieContainer = CookieArray; CookieArray.Add(new Uri("http://club.qingdaonews.com"), new Cookie("PHPSESSID", strCookieSessionID)); CookieArray.Add(new Uri("http://club.qingdaonews.com"), new Cookie("qingdaonews[username]", strCookieUserName)); CookieArray.Add(new Uri("http://club.qingdaonews.com"), new Cookie("qingdaonews[password]", strCookiePassword)); CookieArray.Add(new Uri("http://club.qingdaonews.com"), new Cookie("list_display_time", strList_display_time)); CookieArray.Add(new Uri("http://club.qingdaonews.com"), new Cookie("hiido_tod", "22")); CookieArray.Add(new Uri("http://club.qingdaonews.com"), new Cookie("hiido_ui", "0.9752054967479169")); CookieArray.Add(new Uri("http://club.qingdaonews.com"), new Cookie("hiido_lv", "1258877197212")); CookieArray.Add(new Uri("http://club.qingdaonews.com"), new Cookie("hiido_ti", "1258877197216")); //hiido_tod=22 //hiido_ui=0.9752054967479169 //hiido_lv=1258877197212 //hiido_ti=1258877197216 //获取登录数据流 Stream myRequestStream = LoginHttpWebRequest.GetRequestStream(); //StreamWriter StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")); //把数据写入HttpWebRequest的Request流 myStreamWriter.Write(LoginData); //关闭打开对象 myStreamWriter.Close(); myRequestStream.Close(); //新建一个HttpWebResponse HttpWebResponse myHttpWebResponse = (HttpWebResponse)LoginHttpWebRequest.GetResponse(); //获取一个包含url的Cookie集合的CookieCollection myHttpWebResponse.Cookies = CookieArray.GetCookies(LoginHttpWebRequest.RequestUri); if (myHttpWebResponse.Cookies.Count > 0) { CookieArray.Add(myHttpWebResponse.Cookies); } WebHeaderCollection a = myHttpWebResponse.Headers; Stream myResponseStream = myHttpWebResponse.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312")); strShowBox = myStreamReader.ReadToEnd(); //把数据从HttpWebResponse的Response流中读出 myStreamReader.Close(); myResponseStream.Close(); //======================================================================== //http://club.qingdaonews.com/showAnnounce.php?board_id=2&topic_id=4577258 toolStripStatusLabel1.Text = "发贴成功:http://club.qingdaonews.com/" + strAnnounceID; MessageBox.Show("发贴成功:http://club.qingdaonews.com/" + strAnnounceID); } catch (Exception ex) { throw ex; } }
public static void Add_SameCookieDifferentVairants_OverridesOlderVariant() { Uri uri = new Uri("http://domain.com"); Cookie c1 = new Cookie("name1", "value", "", ".domain.com"); // Variant = Plain Cookie c2 = new Cookie("name1", "value", "", ".domain.com") { Port = "\"80\"" }; // Variant = RFC2965 (should override) Cookie c3 = new Cookie("name1", "value", "", ".domain.com") { Port = "\"80, 90\"" }; // Variant = RFC2965 (should override) Cookie c4 = new Cookie("name1", "value", "", ".domain.com") { Version = 1 }; // Variant = RFC2109 (should be rejected) CookieContainer cc = new CookieContainer(); cc.Add(c1); // Adding a newer variant should override an older one cc.Add(c2); Assert.Equal(c2.Port, cc.GetCookies(uri)[0].Port); // Adding the same variant should override the existing one cc.Add(c3); Assert.Equal(c3.Port, cc.GetCookies(uri)[0].Port); // Adding an older variant shold be rejected cc.Add(c4); Assert.Equal(c3.Port, cc.GetCookies(uri)[0].Port); // Ensure that although we added 3 cookies, only 1 was actually added (the others were overriden or rejected) Assert.Equal(1, cc.Count); }
/// <summary> /// 登录验证 /// </summary> /// <param name="strUserName"></param> /// <param name="strPassword"></param> /// <param name="strLoginUrl"></param> /// <param name="Txt"></param> public void Login(string strUserName, string strPassword, string strLoginUrl, out string Txt) { try { //定义Cookie容器 CookieContainer CookieArray = new CookieContainer(); //创建Http请求 HttpWebRequest LoginHttpWebRequest = (HttpWebRequest)WebRequest.Create(strLoginUrl); //登录数据 string LoginData = "id=" + strUserName + "&passwd=" + strPassword + "&usertype=0"; //数据被传输类型 LoginHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; //数据长度 LoginHttpWebRequest.ContentLength = LoginData.Length; //数据传输方法 get或post LoginHttpWebRequest.Method = "POST"; //设置HttpWebRequest的CookieContainer为刚才建立的那个CookieArray LoginHttpWebRequest.CookieContainer = CookieArray; //获取登录数据流 Stream myRequestStream = LoginHttpWebRequest.GetRequestStream(); //StreamWriter StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312")); //把数据写入HttpWebRequest的Request流 myStreamWriter.Write(LoginData); //关闭打开对象 myStreamWriter.Close(); myRequestStream.Close(); //新建一个HttpWebResponse HttpWebResponse myHttpWebResponse = (HttpWebResponse)LoginHttpWebRequest.GetResponse(); //获取一个包含url的Cookie集合的CookieCollection myHttpWebResponse.Cookies = CookieArray.GetCookies(LoginHttpWebRequest.RequestUri); if (myHttpWebResponse.Cookies.Count > 0) { CookieArray.Add(myHttpWebResponse.Cookies); } string strCookieSessionID = myHttpWebResponse.Cookies[0].Value.ToString(); string strCookieUserName = myHttpWebResponse.Cookies[1].Value.ToString(); string strCookiePassowrd = myHttpWebResponse.Cookies[2].Value.ToString(); WebHeaderCollection a = myHttpWebResponse.Headers; Stream myResponseStream = myHttpWebResponse.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312")); Txt = myStreamReader.ReadToEnd(); if (Txt == "<meta http-equiv=Refresh content=0;URL=login_club_new1.php>") { toolStripStatusLabel1.Text = "Login.OK"; //this.Hide(); //Form2 f2 = new Form2(); //f2.Show(); textUserName.Visible = false; textPassword.Visible = false; button1.Visible = false; label1.Visible = false; label2.Visible = false; //this.Width = 500; //this.Height = 300; listView1.Visible = true; //listView1.Width = 402; //listView1.Height = 214; this.Text = "Sender - " + strUserName; string strOut; LoginSec(strCookieSessionID, strCookieUserName, strCookiePassowrd, out strOut); } else { toolStripStatusLabel1.Text = "Login.ERROR"; } //把数据从HttpWebResponse的Response流中读出 myStreamReader.Close(); myResponseStream.Close(); } catch (Exception ex) { throw ex; } }
public static void GetCookies_DifferentPaths_GetsMatchedPathsIncludingEmptyPath() { // Internally, paths are stored alphabetically sorted - so expect a cookie collection sorted by the path of each cookie // This test ensures that all cookies with paths (that the path specified by the uri starts with) are returned Cookie c1 = new Cookie("name1", "value", "/aa", ".url.com"); Cookie c2 = new Cookie("name2", "value", "/a", ".url.com"); Cookie c3 = new Cookie("name3", "value", "/b", ".url.com"); // Should be ignored - no match with the URL's path Cookie c4 = new Cookie("name4", "value", "/", ".url.com"); // Should NOT be ignored (has no path specified) CookieContainer cc1 = new CookieContainer(); cc1.Add(c1); cc1.Add(c2); cc1.Add(c3); cc1.Add(c4); CookieCollection cc2 = cc1.GetCookies(new Uri("http://url.com/aaa")); Assert.Equal(3, cc2.Count); Assert.Equal(c1, cc2[0]); Assert.Equal(c2, cc2[1]); Assert.Equal(c4, cc2[2]); }
/// <summary> /// Método para validar as credenciais de login do usuário. /// </summary> /// <param name="credential">Objeto que contém informações da credencial do usuário</param> /// <returns>Objeto contendo informações do usuário encontrado, caso não seja encontrado nenhum usuário com correspondente a credencial enviada o método retorna nulo.</returns> private async Task <ClientUser> Authenticate(Credential credential) { // Configurações iniciais string url = "http://identity.sme.prefeitura.sp.gov.br/Account/Login"; CookieContainer cookies = new CookieContainer(); HttpClientHandler handler = new HttpClientHandler(); handler.CookieContainer = cookies; // Inicialização do cliente para requisições (GET e POST) using (HttpClient client = new HttpClient(handler)) using (HttpResponseMessage getResponse = await client.GetAsync(url)) using (HttpContent content = getResponse.Content) { // Extrai o anti forgery token da pagina da requisição GET string result = await content.ReadAsStringAsync(); string forgeryToken = ExtractDataByName(result, "__RequestVerificationToken"); // Faz o POST dos dados (login) caso o usuário não esteja logado if (forgeryToken != string.Empty) { // Cria os dados necessários que compõe o corpo da requisição Dictionary <string, string> data = new Dictionary <string, string>(); data.Add("__RequestVerificationToken", forgeryToken); // Adiciona o Anti Forgery Token data.Add("Username", credential.Username); // Adiciona o nome de usuário data.Add("Password", credential.Password); // Adiciona a senha HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url) { Content = new FormUrlEncodedContent(data) }; // Encoda os dados no formato correto dentro da requisição HttpResponseMessage postResponse = await client.SendAsync(request); // Executa a requisição // Caso a requisição não ocorra corretamente, retorna 'null' if (!postResponse.IsSuccessStatusCode) { return(null); } result = await postResponse.Content.ReadAsStringAsync(); // Caso o usuário não seja autenticado, retorna 'null' if (result.StartsWith("<form method='post' action='http://coresso.sme.prefeitura.sp.gov.br/Login.ashx'>") == false) { return(null); } } // Cria e pega informações do usuário ClientUser user = await GetUser(credential.Username); if (user == null) { user = new ClientUser() { Username = credential.Username } } ; // Pega os cookies da pagina user.Cookies = cookies.GetCookies(new Uri(url)).Cast <Cookie>(); // Preenche as informações do identity user.Identity = new Identity(); user.Identity.code = ExtractDataByName(result, "code"); user.Identity.id_token = ExtractDataByName(result, "id_token"); user.Identity.access_token = ExtractDataByName(result, "access_token"); user.Identity.token_type = ExtractDataByName(result, "token_type"); user.Identity.expires_in = ExtractDataByName(result, "expires_in"); user.Identity.scope = ExtractDataByName(result, "scope"); user.Identity.state = ExtractDataByName(result, "state"); user.Identity.sesion_state = ExtractDataByName(result, "session_state"); return(user); } }
public static void GetCookies_Invalid() { CookieContainer cc = new CookieContainer(); Assert.Throws<ArgumentNullException>(() => cc.GetCookies(null)); }
public static IEnumerable <Cookie> GetCookiesByName(this CookieContainer container, Uri uri, params string[] names) { return(container.GetCookies(uri).Cast <Cookie>().Where(c => names.Contains(c.Name)).ToList()); }