public void Dispose() { if (_raw != null) { _raw.Dispose(); } }
public void DownloadFile(string URL, string filename, ProgressBar prog, Label label) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; float percent = 0; try { System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL); System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; this.Dispatcher.BeginInvoke(new Action(() => { if (prog != null) { prog.Maximum = (int)totalBytes; } })); System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create); long totalDownloadedByte = 0; byte[] by = new byte[1024]; int osize = st.Read(by, 0, (int)by.Length); while (osize > 0) { totalDownloadedByte = osize + totalDownloadedByte; System.Windows.Forms.Application.DoEvents(); so.Write(by, 0, osize); this.Dispatcher.BeginInvoke(new Action(() => { if (prog != null) { prog.Value = (int)totalDownloadedByte; } })); osize = st.Read(by, 0, (int)by.Length); percent = (float)totalDownloadedByte / (float)totalBytes * 100; this.Dispatcher.BeginInvoke(new Action(() => { label.Content = "正在下载..." + percent.ToString() + "% " + System.IO.Path.GetFileName(URL); })); } myrp.Dispose(); so.Close(); st.Close(); DownloadedCount += 1; } catch (System.Exception) { DownloadedCount += 1; } }
protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { _httpWebResponse.Dispose(); _httpWebRequest.Dispose(); } disposedValue = true; } }
/// <summary> /// Method like destructor to dispose the class. /// </summary> public void Dispose() { dlTimer.Stop(); dsTimer.Stop(); if (fStream != null) { fStream.Dispose(); } if (res != null) { res.Dispose(); } if (stream != null) { stream.Dispose(); } System.GC.SuppressFinalize(this); }
private string ReadPage(HttpWebResponse response) { string html = String.Empty; try { using (Stream inputStream = response.GetResponseStream()) using (StreamReader reader = new StreamReader(inputStream)) { html = reader.ReadToEnd(); } response.Dispose(); } catch(NullReferenceException ex) { Console.WriteLine(ex.Message); } return html; }
internal static async Task <HttpResponseInfo> SendAsync(this LazyUri url, WebRequestOptions options, HttpRequestMessageBox messageBox, bool alwaysCatchAndForbidRedirects = false, bool keepResponseAliveOnError = false, bool synchronous = false) { HttpUtils.EnsureInitialized(); if (!synchronous) { await Utils.CheckLocalFileAccessAsync(url); } Utils.RaiseWebRequestEvent(url, false); HttpResponseMessage result = null; LazyUri previousResponse2 = null; try { if (options == WebRequestOptions.DefaultOptions) { throw new ArgumentException(); } if (options.WaitBefore.Ticks != 0 && !synchronous) { await TaskEx.Delay(options.WaitBefore); } LazyUri previousResponse1 = null; previousResponse2 = url.Clone(); previousResponse2 = MaybeAddAdditionalQueryParameters(previousResponse2, options); var redirectIndex = 0; while (true) { #if WEBCLIENT HttpContent requestContent = null; #endif var message = messageBox?.PrebuiltRequest ?? CreateRequestInternal(previousResponse2, options, true, redirectIndex #if WEBCLIENT , out requestContent #endif ); if (messageBox != null) { messageBox.Dispose(); messageBox.Message = message; } #if WEBCLIENT if (requestContent != null) { if (requestContent.ContentType != null) { message.ContentType = requestContent.ContentType; } if (requestContent.ContentDisposition != null) { message.Headers["Content-Disposition"] = requestContent.ContentDisposition; } using (var req = await message.GetRequestStreamAsync()) { await requestContent.CopyToAsync(req); } } result = (HttpWebResponse)await message.GetResponseAsync(); #else message.Properties["ShamanURL"] = url; if (options.CustomHttpClient != null) { result = await options.CustomHttpClient.SendAsync(message, HttpCompletionOption.ResponseHeadersRead); } else { if (defaultHttpClient == null) { defaultHttpClient = CreateHttpClient(); } result = messageBox?.PrebuiltResponse ?? await defaultHttpClient.SendAsync(message, HttpCompletionOption.ResponseHeadersRead); } #endif #if !WEBCLIENT if (result.Content != null && result.Content.Headers.ContentType != null && result.Content.Headers.ContentType.CharSet == "utf8") { result.Content.Headers.ContentType.CharSet = "utf-8"; } #endif if ((int)result.StatusCode >= 400) { if (!keepResponseAliveOnError) { result.Dispose(); } // Hackish, change purpose of enumeration type throw new WebException("The web server returned: " + result.StatusCode.ToString(), (WebExceptionStatus)result.StatusCode); } #if WEBCLIENT var zz = result.Headers["Location"]; var redirectUrlNative = zz != null?HttpUtils.GetAbsoluteUri(url.PathConsistentUrl, zz) : null; #else var redirectUrlNative = result.Headers.Location; #endif if (redirectUrlNative == null) { return(new HttpResponseInfo() { RespondingUrl = previousResponse2, Response = result }); } else { if (alwaysCatchAndForbidRedirects) { return new HttpResponseInfo() { Response = result, RespondingUrl = previousResponse2, Exception = new WebException("Unexpected redirect", HttpUtils.Error_UnexpectedRedirect) } } ; result.Dispose(); var redirectUrl = new LazyUri(redirectUrlNative); if (!redirectUrl.IsAbsoluteUri) { redirectUrl = new LazyUri(new Uri(previousResponse2.PathConsistentUrl, redirectUrlNative)); } if (options != null && !options.AllowRedirects) { throw new WebException("Unexpected redirect was received.", HttpUtils.Error_UnexpectedRedirect); } if (redirectIndex == Configuration_MaximumNumberOfRedirects) { throw new WebException("The maximum number of redirects has been reached.", HttpUtils.Error_MaximumNumberOfRedirectsExceeded); } if (!(redirectIndex == 0 && options != null && (options.PostData != null || options.PostString != null))) { if (( (previousResponse1 != null && HttpUtils.UrisEqual(redirectUrl.PathAndQueryConsistentUrl, previousResponse1.PathAndQueryConsistentUrl)) || HttpUtils.UrisEqual(redirectUrl, previousResponse2))) { if (url.GetFragmentParameter("$allow-same-redirect") == "1") { if (!synchronous) { #if NET35 await TaskEx.Delay(Configuration_SameRedirectDelayTimeMs); #else await Task.Delay(Configuration_SameRedirectDelayTimeMs); #endif } } else { throw new WebException("The server isn't redirecting the requested resource properly.", HttpUtils.Error_RedirectLoopDetected); } } } previousResponse1 = previousResponse2; previousResponse2 = redirectUrl; redirectIndex++; } } } catch (Exception ex) { var orig = ex; #if !WEBCLIENT var hre = ex as HttpRequestException; if (hre != null && hre.InnerException != null) { ex = hre.InnerException; } #endif if (alwaysCatchAndForbidRedirects) { return new HttpResponseInfo() { Exception = ex, Response = result, RespondingUrl = previousResponse2 } } ; else if (ex == orig) { throw; } else { throw ex.Rethrow(); } } }
/// <summary> /// Parses the DAV result. /// Note that it will only parse if the server status was 207. /// There maybe error outputs on 4xx and 5xx but this will not parsed /// by this class. /// </summary> /// <param name="request"></param> /// <param name="response"></param> /// <param name="uri"></param> public DAVRequestResult(WebDAV request, HttpWebResponse response, Uri uri) { Request = request; Items = new List<Item>(); Status = (ServerStatus)Enum.Parse(typeof(ServerStatus), response.StatusCode.ToString(), false); StatusText = response.StatusDescription; IsMultiState = Status == ServerStatus.MultiStatus; _stream = new MemoryStream(); response.GetResponseStream().CopyTo(_stream); // dispose response.Dispose(); _stream.Seek(0, SeekOrigin.Begin); if (_stream.Length == 0) return; // A kingdom for normal DOMDocument support. // Why not XDocument? XmlReader is faster and less resource hungry. // A huge multitstatus would be first loaded to memory completely. // // This reader can only go forward. Read-* methods will cause // to jump over elements. Hence we stop at the element and // store the element name. Then wait for Text-Elements value // to capture. using(XmlReader reader = XmlReader.Create(_stream, null)) { Item item = new Item(); var waitForResourceType = false; var lastElementName = ""; var waitForLockScope = false; var waitForLockType = false; List<DAVLocking> lockingList = null; List<PropertyState> propertyStateList = null; PropertyState pItem = null; DAVLocking litem = null; while (reader.Read()) { switch (reader.NodeType) { // look for special elements case XmlNodeType.Element: if (reader.NamespaceURI == XmlNamespaces.NsDav) { switch (reader.LocalName) { // DAV Elements // Response case Elements.Response: // start a new item // pItem must be set before d:prop in order to // catch non-real properties such "href" item = new Item(); propertyStateList = new List<PropertyState>(); pItem = new PropertyState(); break; // Resource type case Elements.Collection: if (waitForResourceType) { item.ResourceType = ResourceType.Collection; } break; // Lock case Elements.LockEntry: litem = new DAVLocking(); lockingList.Add(litem); break; case Elements.LockScope: waitForLockScope = true; break; case Elements.LockType: waitForLockType = true; break; case Elements.ExclusiveLocking: if (waitForLockScope) { litem.Scope = DAVLocking.LockScope.Exclusive; } break; case Elements.SharedLocking: if (waitForLockScope) { litem.Scope = DAVLocking.LockScope.Shared; } break; case Elements.WriteLocking: if (waitForLockType) { litem.Type = DAVLocking.LockType.Write; } break; case Elements.LockDiscovery: ///TODO break; // DAV Properties case Elements.Properties: // a pItem was already created before break; case Properties.ResourceType: waitForResourceType = true; break; case Properties.SupportedLock: lockingList = new List<DAVLocking>(); break; default: lastElementName = reader.LocalName; break; } } break; // clean up case XmlNodeType.EndElement: if (reader.NamespaceURI == XmlNamespaces.NsDav) { switch (reader.LocalName) { // DAV Elements case Elements.PropertyState: // save to list and create a new one (which stays maybe temporary) propertyStateList.Add(pItem); pItem = new PropertyState(); break; case Elements.Response: // clean the list // the HTTP Status is important foreach (PropertyState state in propertyStateList) { if (state.Status == ServerStatus.OK) { item.Properties = state.Properties; } else { item.FailedProperties.Add(state); } } // Close the item Items.Add(item); item = null; // Reset the property state list propertyStateList = null; pItem = null; break; // Locking case Elements.LockType: waitForLockType = false; break; case Elements.LockScope: waitForLockScope = false; break; // DAV Properties case Properties.ResourceType: waitForResourceType = false; break; case Properties.SupportedLock: item.Locking = lockingList; break; } } break; // Grap the text values case XmlNodeType.Text: // no whitespace please if (reader.Value == null) continue; // can't set in empty element if (item == null) continue; switch (lastElementName) { // DAV Elements case Elements.Reference: string _ref = Uri.UnescapeDataString(reader.Value); pItem.Properties.Add(lastElementName, _ref); pItem.Properties.Add(lastElementName + ".local", _ref.Trim('/').Split('/').Last()); break; // Status element case Elements.Status: List<string> s = new List<string>(reader.Value.Split(' ')); s.RemoveAt(0); pItem.Status = (ServerStatus)Enum.Parse(typeof(ServerStatus), s[0], false); s.RemoveAt(0); pItem.ServerStatusText = String.Join(" ", s.ToArray()); break; // DAV Properties case Properties.QuotaUsedBytes: case Properties.QuotaAvailableBytes: case Properties.GetContentLength: pItem.Properties.Add(lastElementName, long.Parse(reader.Value)); break; case Properties.DisplayName: case Properties.GetContentLanguage: case Properties.GetContentType: case Properties.GetETag: pItem.Properties.Add(lastElementName, reader.Value); break; case Properties.GetLastModified: case Properties.CreationDate: pItem.Properties.Add(lastElementName, DateTime.Parse(reader.Value)); break; } lastElementName = ""; break; } } } }
private void ConnectExceptionCleanup(HttpWebResponse response) { Dispose(); if (response != null) { response.Dispose(); } }
/// <summary> /// Processes the content of response received in context of given request. /// </summary> protected void ProcessResponse(HttpWebRequest request, HttpWebResponse response, HttpDataSourceResponseType responseType, Exception ex) { // data reception failed or timeouted/cancelled? if (response == null) { string statusDescription = ex != null ? string.IsNullOrEmpty(ex.Message) ? ex.GetType().Name : string.Concat(ex.GetType().Name, ": ", ex.Message) : "Unknown error"; ProcessFailedResponse(request, HttpStatusCode.ServiceUnavailable, statusDescription); return; } ProcessSuccessfulResponse(request, response, responseType); #if WINDOWS_STORE response.Dispose(); #else response.Close(); #endif }
static void Main(string[] args) { string base_url = "http://www.footlocker.com"; HttpWebRequest initialRequest = (HttpWebRequest)WebRequest.Create(base_url); initialRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; initialRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"; //initialRequest.Headers.Add("DNT", "1"); initialRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; initialRequest.Headers.Add("Upgrade-Insecure-Requests", "1"); initialRequest.Headers.Add("Accept-Encoding", "gzip, deflate, sdch"); initialRequest.Headers.Add("Accept-Language", "en-US,en;q=0.8,da;q=0.6"); initialRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; initialRequest.Method = "GET"; initialRequest.Credentials = CredentialCache.DefaultCredentials; initialRequest.CookieContainer = new CookieContainer(); System.Net.HttpWebResponse initialResponse = (HttpWebResponse)initialRequest.GetResponse(); var initialCookies = initialResponse.Cookies; initialResponse.Dispose(); string url = "http://www.footlocker.com/product/model:190074/sku:55088011/jordan-retro-1-high-og-mens/black/white/?cm="; //string url = "http://www.footlocker.com/product/model:201111/sku:54861001/nike-sb-stefan-janoski-boys-grade-school/black/white/?cm="; string size = "7.0"; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Credentials = CredentialCache.DefaultCredentials; request.Method = "GET"; request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.2 (KHTML, like Gecko) Chrome/15.0.874.121 Safari/535.2"; request.Headers.Add("DNT", "1"); request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"; request.Headers.Add("Accept-Encoding", "gzip, deflate, sdch"); request.Headers.Add("Accept-Language", "en-US,en;q=0.8,da;q=0.6"); request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { var setcookie = response.Headers.Get("Set-Cookie"); var cookies = request.Headers.AllKeys; // Display the status. Console.WriteLine(((HttpWebResponse)response).StatusDescription); // Get the stream containing content returned by the server. using (Stream dataStream = response.GetResponseStream()) { // Open the stream using a StreamReader for easy access. using (StreamReader reader = new StreamReader(dataStream)) { // Read the content. string responseFromServer = reader.ReadToEnd(); // Display the content. //Console.WriteLine(responseFromServer); var doc = new HtmlDocument(); doc.LoadHtml(responseFromServer); HtmlNode pdp_selectedSKU = doc.GetElementbyId("pdp_selectedSKU"); var realSkuValue = pdp_selectedSKU.GetAttributeValue("value", string.Empty); HtmlNode pdp_model = doc.GetElementbyId("pdp_model"); var realModelValue = pdp_model.GetAttributeValue("value", string.Empty); HtmlNode requestKey = doc.GetElementbyId("requestKey"); var realRequestKeyValue = requestKey.GetAttributeValue("value", string.Empty); string postUrl = "http://www.footlocker.com/catalog/miniAddToCart.cfm?secure=0"; request.Headers.Clear(); request = (HttpWebRequest)WebRequest.Create(postUrl); request.Method = "POST"; request.Credentials = CredentialCache.DefaultNetworkCredentials; request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; //request.ContentType = "application/x-www-form-urlencoded"; //request.Headers.Add("Cookie", setcookie); //string setcookie.Split(new char[]{ ';' }); Dictionary <string, string> payload = CreatePayload(realRequestKeyValue, realSkuValue, realModelValue); request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"; request.Accept = "*/*"; request.Headers.Add("Origin", "http://www.footlocker.com"); request.Headers.Add("X-Requested-With", "XMLHttpRequest"); request.Referer = url; request.Headers.Add("Accept-Encoding", "gzip, deflate"); request.Headers.Add("Accept-Language", "en-US,en;q=0.8"); request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"; request.Host = "www.footlocker.com"; request.KeepAlive = true; request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(new Uri("http://www.footlocker.com"), initialCookies); string postData = ""; foreach (string key in payload.Keys) { postData += HttpUtility.UrlEncode(key) + "=" + HttpUtility.UrlEncode(payload[key]) + "&"; } postData += "inlineAddToCart=1"; byte[] data = Encoding.ASCII.GetBytes(postData); Stream requestStream2 = request.GetRequestStream(); requestStream2.Write(data, 0, data.Length); requestStream2.Close(); HttpWebResponse myHttpWebResponse = (HttpWebResponse)request.GetResponse(); Stream responseStream = myHttpWebResponse.GetResponseStream(); StreamReader myStreamReader = new StreamReader(responseStream, Encoding.Default); string pageContent = myStreamReader.ReadToEnd(); myStreamReader.Close(); // responseStream.Close(); myHttpWebResponse.Close(); } } } Console.Read(); }
private void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse) { using (webResponse) { #if FRAMEWORK response.ContentEncoding = webResponse.ContentEncoding; response.Server = webResponse.Server; #endif response.ContentType = webResponse.ContentType; response.ContentLength = webResponse.ContentLength; Stream webResponseStream = webResponse.GetResponseStream(); #if WINDOWS_PHONE if (String.Equals(webResponse.Headers[HttpRequestHeader.ContentEncoding], "gzip", StringComparison.OrdinalIgnoreCase)) { var gzStream = new GZipStream(webResponseStream); ProcessResponseStream(gzStream, response); } else { ProcessResponseStream(webResponseStream, response); } #else ProcessResponseStream(webResponseStream, response); #endif response.StatusCode = webResponse.StatusCode; response.StatusDescription = webResponse.StatusDescription; response.ResponseUri = webResponse.ResponseUri; response.ResponseStatus = ResponseStatus.Completed; #if !PocketPC if (webResponse.Cookies != null) { foreach (Cookie cookie in webResponse.Cookies) { response.Cookies.Add(new HttpCookie { Comment = cookie.Comment, CommentUri = cookie.CommentUri, Discard = cookie.Discard, Domain = cookie.Domain, Expired = cookie.Expired, Expires = cookie.Expires, HttpOnly = cookie.HttpOnly, Name = cookie.Name, Path = cookie.Path, Port = cookie.Port, Secure = cookie.Secure, TimeStamp = cookie.TimeStamp, Value = cookie.Value, Version = cookie.Version }); } } #endif foreach (var headerName in webResponse.Headers.AllKeys) { var headerValue = webResponse.Headers[headerName]; response.Headers.Add(new HttpHeader { Name = headerName, Value = headerValue }); } #if WINDOWS_UAP webResponse.Dispose(); #else webResponse.Close(); #endif } }