Нет это видимо чисто мой даун так детектится, создал левое приложение которое скачивает всё норм. Видимо определяют что с обменников качаю. В надо пробывать с архивом может прокатит.
Вот мой клиент который качает, бывает сменишь и кричать перестают, может пригодится кому:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
using System.Security;
using System.Threading;
namespace WhiteNet.Http
{
class HttpClient : ICloneable
{
private Dictionary<string, string> RequestProperies = new Dictionary<string, string>(); // свойства
public WebHeaderCollection ResponseHeaders = new WebHeaderCollection();
public WebHeaderCollection RequestHeaders = new WebHeaderCollection();
public Dictionary<string, string> SetCookies = new Dictionary<string, string>();
public string sProxy ; // прокси
public bool UseProxy; // использовать прокси
public bool AllowControlRedirect = true;
public bool AllowAutoRedirect = false;
public bool UseCookies; // использовать куки
public Encoding encoding = Encoding.UTF8 ; // кодировка
private HttpStatusCode responseCode ;
private string responseUrl ;
public int DownloadStep = 1024;
public readonly string ID;
public object Clone()
{
HttpClient httpClient = (HttpClient) this.MemberwiseClone();
httpClient.cookies = new CookieCollection();
httpClient.cookies.Add(this.cookies);
httpClient.RequestProperies = new Dictionary<string, string>();
httpClient.RequestHeaders = new WebHeaderCollection();
httpClient.RequestHeaders.Add(this.RequestHeaders);
httpClient.UseCookies = this.UseCookies;
foreach (KeyValuePair<string, string> pair in RequestProperies)
{
httpClient.RequestProperies.Add(pair.Key , pair.Value);
}
foreach (KeyValuePair<string, string> pair in SetCookies)
{
httpClient.SetCookies.Add(pair.Key , pair.Value);
}
httpClient.encoding = this.encoding;
return httpClient;
}
public HttpStatusCode ResponseCode
{
get
{
return responseCode;
}
}
public string ResponseUrl
{
get
{
return responseUrl;
}
}
#region Cookies
private CookieCollection cookies = new CookieCollection();
public string CookieText()
{
string res = String.Empty;
foreach (KeyValuePair <string , string> cook in SetCookies)
{
res += cook.Key + "=" + cook.Value + "; ";
}
return res;
}
public string GetCookie(int index)
{
if (index >= cookies.Count) throw new IndexOutOfRangeException();
return cookies[index].Value;
}
public string GetCookie (string key)
{
return cookies [key].Value;
}
#endregion
public HttpClient()
{
AllowControlRedirect = true;
UseProxy = false;
UseCookies = true;
}
public HttpClient(string id)
{
AllowControlRedirect = true;
UseProxy = false;
UseCookies = true;
ID = id;
}
#region Headers
public void SetProperty(string key, string value)
{
RequestProperies[key.ToLower()] = value;
}
public void ClearHeaders()
{
RequestProperies.Clear();
}
public void ParseCookies(String setCookie)
{
if (setCookie == null) return;
string[] scookies = setCookie.Split(',');
if (scookies != null)
{
foreach (string s in scookies)
{
string[] vals = s.Split(';');
if (vals.Length > 1 && vals[0].Contains("="))
{
int p = vals[0].IndexOf("=");
string key = vals[0].Substring(0 , p ).Trim();
string value = vals[0].Substring(p + 1, vals[0].Length - p - 1 ).Trim();
SetCookies[key] = value;
}
}
}
}
public void AddCookies(string Cookie)
{
if (Cookie == null ) return;
string[] cookies = Cookie.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
if (cookies == null) return;
foreach (String s in cookies)
{
string[] values = s.Split('=');
if (values != null && values.Length >= 2)
{
SetCookies[values[0]] = values[1];
}
}
}
private void AddCookies()
{
this.RequestHeaders["Cookie"] = String.Empty;
foreach (KeyValuePair<string, string> pair in SetCookies)
{
RequestHeaders["Cookie"] += pair.Key + "=" + pair.Value + "; ";
}
}
private HttpWebRequest CreateHttpWebRequest(string sUrl, string method)
{
if (!Uri.IsWellFormedUriString(sUrl, UriKind.Absolute))
throw new UriFormatException();
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(sUrl);
httpWebRequest.AllowAutoRedirect = AllowAutoRedirect;
httpWebRequest.Method = method;
if (sUrl.Contains("https")) SetCredencials(httpWebRequest);
if (UseProxy)
{
SetProxy(httpWebRequest);
SetCredencials(httpWebRequest);
if (sUrl.Contains("https")) httpWebRequest.Proxy.Credentials = CredentialCache.DefaultNetworkCredentials;
httpWebRequest.PreAuthenticate = true;
}
else httpWebRequest.Proxy = null;
if (UseCookies) AddCookies();
httpWebRequest.Headers.Add(RequestHeaders); // добавляем заголовки в запрос
foreach (KeyValuePair<string , string> pair in RequestProperies)
{
if (pair.Key == "useragent") httpWebRequest.UserAgent = pair.Value;
else if (pair.Key == "accept") httpWebRequest.Accept = pair.Value;
else if (pair.Key == "referer") httpWebRequest.Referer = pair.Value;
else if (pair.Key == "contenttype") httpWebRequest.ContentType = pair.Value;
else if (pair.Key == "contentlength") httpWebRequest.ContentLength = long.Parse(pair.Value);
else if (pair.Key == "timeout") httpWebRequest.ReadWriteTimeout = int.Parse(pair.Value);
else if (pair.Key == "expect") httpWebRequest.Expect = pair.Value;
}
return httpWebRequest;
}
#endregion
#region Post and Get Method
// получение данных страницы
public string Get(string sUrl)
{
StartRequest();
try
{
IAsyncResult ar = BeginGetHTML(sUrl, null, null, null);
return EndGetHTML(ar);
}
finally
{
EndRequest();
}
}
public string Post(string sUrl, string sPostData)
{
StartRequest();
try
{
IAsyncResult ar = BeginPost(sUrl, sPostData, null, null, null);
return EndPost(ar);
}
finally
{
EndRequest();
}
}
public void Get(string sUrl, Stream stream)
{
StartRequest();
try
{
IAsyncResult ar = BeginGetFile (sUrl, stream , null, null, null);
EndGetFile (ar);
}
finally
{
EndRequest();
}
}
public string Post(string sUrl, Stream upStream)
{
StartRequest();
try
{
IAsyncResult ar = BeginPost(sUrl, upStream, null, null, null);
return EndPost(ar);
}
finally
{
EndRequest();
}
}
#endregion
#region AsycMethods
public delegate void ErrorHandler(Exception ex);
internal sealed class HttpRequestState
{
public ManualResetEvent manualEvent;
public HttpWebRequest httpWebRequest;
public HttpWebResponse response;
public Stream responseStream;
public Stream returnStream = null;
public AsyncCallback userCallBack;
public object userState;
public ErrorHandler OnError;
public string requestUrl;
public byte[] buffer = null;
public int bytesRead = 0;
public long contentLen;
public Stream uploadStream;
public Stream queryStream;
public HttpRequestState(int bufferSize)
{
manualEvent = new ManualResetEvent(false);
buffer = new byte [bufferSize];
}
}
sealed class HttpAsyncResult : IAsyncResult
{
WaitHandle asyncWaitHandle;
object requestState;
public object userState;
public HttpAsyncResult(WaitHandle waitHandle, object state)
{
asyncWaitHandle = waitHandle;
requestState = state;
}
public bool IsCompleted
{
get { return asyncWaitHandle.WaitOne(1 , false); }
}
public WaitHandle AsyncWaitHandle
{
get { return asyncWaitHandle; }
}
public bool CompletedSynchronously
{
get { return false; }
}
public object AsyncState
{
get { return userState; }
}
public object RequestState
{
get { return requestState; }
}
}
HttpRequestState CreateRequestState(HttpWebRequest httpWebRequest , Stream stream, AsyncCallback userCallBack, object userState, ErrorHandler errorCallBack, string sUrl)
{
HttpRequestState httpRequestState = new HttpRequestState(DownloadStep);
httpRequestState.httpWebRequest = httpWebRequest;
httpRequestState.userCallBack = userCallBack;
httpRequestState.OnError = errorCallBack;
httpRequestState.userState = userState;
httpRequestState.requestUrl = sUrl;
httpRequestState.returnStream = (stream == null) ? new MemoryStream() : stream;
return httpRequestState;
}
public IAsyncResult BeginGetHTML (string sUrl, AsyncCallback userCallBack, object userState, ErrorHandler errorCallBack)
{
HttpWebRequest httpWebRequest = CreateHttpWebRequest(sUrl, "GET");
HttpRequestState httpRequestState = CreateRequestState(httpWebRequest, null, userCallBack, userState, errorCallBack, sUrl);
HttpAsyncResult asyncResult = new HttpAsyncResult(httpRequestState.manualEvent, httpRequestState);
asyncResult.userState = userState;
StartRequest();
httpWebRequest.BeginGetResponse(new AsyncCallback(RespCallBack), httpRequestState);
if (userCallBack != null) userCallBack.BeginInvoke(asyncResult, null, null);
return asyncResult;
}
public IAsyncResult BeginGetFile (string sUrl, Stream stream , AsyncCallback userCallBack, object userState, ErrorHandler errorCallBack)
{
HttpWebRequest httpWebRequest = CreateHttpWebRequest(sUrl, "GET");
HttpRequestState httpRequestState = CreateRequestState(httpWebRequest, stream, userCallBack, userState, errorCallBack, sUrl);
HttpAsyncResult asyncResult = new HttpAsyncResult(httpRequestState.manualEvent, httpRequestState);
asyncResult.userState = userState;
StartRequest();
httpWebRequest.BeginGetResponse(new AsyncCallback(RespCallBack), httpRequestState);
if (userCallBack != null) userCallBack.BeginInvoke(asyncResult, null, null);
return asyncResult;
}
public IAsyncResult BeginPost (string sUrl, string sPost , AsyncCallback userCallBack, object userState, ErrorHandler errorCallBack)
{
HttpWebRequest httpWebRequest = CreateHttpWebRequest(sUrl, "POST");
HttpRequestState httpRequestState = CreateRequestState(httpWebRequest, null, userCallBack, userState, errorCallBack, sUrl);
HttpAsyncResult asyncResult = new HttpAsyncResult(httpRequestState.manualEvent, httpRequestState);
asyncResult.userState = userState;
StartRequest();
if (userCallBack != null) userCallBack.BeginInvoke(asyncResult, null, null);
httpRequestState.uploadStream = new MemoryStream();
byte[] buffer = encoding.GetBytes(sPost);
httpRequestState.uploadStream.Write(buffer , 0 , buffer.Length);
httpWebRequest.BeginGetRequestStream(new AsyncCallback(GetRequestCallback) , httpRequestState);
return asyncResult;
}
public IAsyncResult BeginPost(string sUrl, Stream upStream , AsyncCallback userCallBack, object userState, ErrorHandler errorCallBack)
{
HttpWebRequest httpWebRequest = CreateHttpWebRequest(sUrl, "POST");
httpWebRequest.ContentLength = upStream.Length;
HttpRequestState httpRequestState = CreateRequestState(httpWebRequest, null, userCallBack, userState, errorCallBack, sUrl);
HttpAsyncResult asyncResult = new HttpAsyncResult(httpRequestState.manualEvent, httpRequestState);
asyncResult.userState = userState;
StartRequest();
if (userCallBack != null) userCallBack.BeginInvoke(asyncResult, null, null);
httpRequestState.uploadStream = upStream;
httpWebRequest.BeginGetRequestStream(new AsyncCallback(GetRequestCallback), httpRequestState);
return asyncResult;
}
public IAsyncResult BeginPut (string sUrl, Stream upStream, AsyncCallback userCallBack, object userState, ErrorHandler errorCallBack)
{
HttpWebRequest httpWebRequest = CreateHttpWebRequest(sUrl, "PUT");
httpWebRequest.ContentLength = upStream.Length;
HttpRequestState httpRequestState = CreateRequestState(httpWebRequest, null, userCallBack, userState, errorCallBack, sUrl);
HttpAsyncResult asyncResult = new HttpAsyncResult(httpRequestState.manualEvent, httpRequestState);
asyncResult.userState = userState;
StartRequest();
if (userCallBack != null) userCallBack.BeginInvoke(asyncResult, null, null);
httpRequestState.uploadStream = upStream;
httpWebRequest.BeginGetRequestStream(new AsyncCallback(GetRequestCallback), httpRequestState);
// System.Windows.Forms.MessageBox.Show("put");
return asyncResult;
}
void GetRequestCallback(IAsyncResult ar)
{
HttpRequestState httpRequestState = ar.AsyncState as HttpRequestState ;
try
{
Stream queryStream = httpRequestState.httpWebRequest.EndGetRequestStream(ar);
httpRequestState.queryStream = queryStream;
httpRequestState.uploadStream.Seek(0 , SeekOrigin.Begin);
httpRequestState.uploadStream.BeginRead( httpRequestState.buffer , 0 , DownloadStep ,
new AsyncCallback(WriteCallback), httpRequestState );
}
catch (Exception ex)
{
EndRequest();
httpRequestState.manualEvent.Set();
if (httpRequestState.OnError != null)
httpRequestState.OnError.Invoke(ex);
}
}
void WriteCallback(IAsyncResult ar)
{
HttpRequestState httpRequestState = (HttpRequestState)ar.AsyncState;
try
{
int bytesRead = httpRequestState.uploadStream.EndRead(ar);
if (bytesRead > 0)
{
httpRequestState.bytesRead += bytesRead;
httpRequestState.queryStream.Write(httpRequestState.buffer, 0, bytesRead);
bool isInterrupted = false;
this.DoProgress(httpRequestState.bytesRead, httpRequestState.uploadStream.Length, ref isInterrupted , "upload");
if (!isInterrupted)
{
httpRequestState.uploadStream.BeginRead(httpRequestState.buffer, 0, DownloadStep,
new AsyncCallback(WriteCallback), httpRequestState);
return;
}
}
httpRequestState.uploadStream.Close();
httpRequestState.queryStream.Close();
httpRequestState.httpWebRequest.BeginGetResponse(new AsyncCallback(RespCallBack), httpRequestState);
}
catch (Exception ex)
{
EndRequest();
httpRequestState.manualEvent.Set();
if (httpRequestState.OnError != null)
httpRequestState.OnError.Invoke(ex);
}
}
void RespCallBack(IAsyncResult ar)
{
HttpRequestState requestState = ar.AsyncState as HttpRequestState;
try
{
HttpWebResponse httpWebResponse = (HttpWebResponse)requestState.httpWebRequest.EndGetResponse(ar);
requestState.response = httpWebResponse;
responseUrl = httpWebResponse.ResponseUri.AbsoluteUri;
long len = httpWebResponse.ContentLength;
ResponseHeaders.Clear();
ResponseHeaders.Add(httpWebResponse.Headers);
responseCode = httpWebResponse.StatusCode;
if (len == -1)
try
{
len = long.Parse(httpWebResponse.Headers.Get("Content-Length"));
}
catch (ArgumentNullException)
{
len = 0;
}
requestState.contentLen = len;
if (UseCookies )
{
string sCookie = httpWebResponse.Headers["Set-Cookie"];
ParseCookies(sCookie);
}
if (httpWebResponse.StatusCode == HttpStatusCode.Redirect && AllowControlRedirect && !AllowAutoRedirect)
{
bool Handled = false;
string path = httpWebResponse.Headers["location"];
this.SetProperty("Referer", requestState.requestUrl);
if (!path.Contains("http") && !path.Contains ("https"))
{
path = (requestState.requestUrl.Contains("https://")) ?
"https://" + new Uri(requestState.requestUrl).Authority + path :
"http://" + new Uri(requestState.requestUrl).Authority + path;
}
DoRedirect(requestState.httpWebRequest, httpWebResponse, ref Handled, ref path);
if (!Handled)
{
httpWebResponse.Close();
httpWebResponse = null;
HttpWebRequest httpWebRequest = CreateHttpWebRequest(path, "GET");
requestState.httpWebRequest = httpWebRequest;
requestState.requestUrl = path;
httpWebRequest.BeginGetResponse(new AsyncCallback(RespCallBack), requestState);
return;
}
}
requestState.responseStream = (Stream)httpWebResponse.GetResponseStream();
requestState.bytesRead = 0;
requestState.responseStream.BeginRead(requestState.buffer , 0 , requestState.buffer.Length, ReadCallback , requestState);
}
catch (Exception ex)
{
EndRequest();
requestState.manualEvent.Set();
if (requestState.OnError != null)
requestState.OnError.Invoke(ex);
}
}
public void ReadCallback(IAsyncResult ar)
{
HttpRequestState httpRequestState = (HttpRequestState)ar.AsyncState;
try
{
int bytesRead = httpRequestState.responseStream.EndRead(ar);
if (bytesRead > 0)
{
httpRequestState.bytesRead += bytesRead;
httpRequestState.returnStream.Write(httpRequestState.buffer, 0, bytesRead);
bool isInterrupted = false;
this.DoProgress(httpRequestState.bytesRead, httpRequestState.contentLen, ref isInterrupted , "download");
if (!isInterrupted)
{
httpRequestState.responseStream.BeginRead(httpRequestState.buffer, 0, httpRequestState.buffer.Length, ReadCallback, httpRequestState);
return;
}
}
httpRequestState.responseStream.Close();
httpRequestState.response.Close();
httpRequestState.manualEvent.Set();
EndRequest();
}
catch (Exception ex)
{
httpRequestState.manualEvent.Set();
EndRequest();
if (httpRequestState.OnError != null)
httpRequestState.OnError.Invoke(ex);
}
}
public String EndGetHTML (IAsyncResult ar)
{
HttpAsyncResult httpAsyncRes = ar as HttpAsyncResult;
if (httpAsyncRes == null)
throw new System.ArgumentException();
HttpRequestState httpRequestState = httpAsyncRes.RequestState as HttpRequestState;
httpRequestState.manualEvent.WaitOne();
httpRequestState.returnStream.Seek(0, SeekOrigin.Begin);
try
{
using (StreamReader reader = new StreamReader(httpRequestState.returnStream, encoding))
{
return reader.ReadToEnd();
}
}
finally
{
httpRequestState.returnStream.Close();
}
}
public String EndPost (IAsyncResult ar)
{
return EndGetHTML(ar);
}
public String EndPut(IAsyncResult ar)
{
return EndGetHTML(ar);
}
public Stream EndGetFile (IAsyncResult ar)
{
HttpAsyncResult httpAsyncRes = ar as HttpAsyncResult;
if (httpAsyncRes == null)
throw new System.ArgumentException();
HttpRequestState httpRequestState = httpAsyncRes.RequestState as HttpRequestState;
httpRequestState.manualEvent.WaitOne();
return httpRequestState.returnStream;
}
#endregion
#region ClassEvents
public class RedirectEventArgs : EventArgs
{
public readonly HttpWebRequest httpWebRequest;
public readonly HttpWebResponse httpWebResponse;
public bool Handled;
public string Path;
public RedirectEventArgs (HttpWebRequest httpWebRequest , HttpWebResponse httpWebResponse, string path)
{
this.httpWebRequest = httpWebRequest;
this.httpWebResponse = httpWebResponse;
Handled = false;
Path = path;
}
}
public class DownloadEventArgs : EventArgs
{
public readonly long bytesRead, bytesAll;
public readonly string what;
public bool IsInterrupted = false;
public DownloadEventArgs (long br , long ba, string what)
{
bytesRead = br;
bytesAll = ba;
this.what = what;
}
}
public delegate void ProgressEventHandler (object sender, DownloadEventArgs e); // обработчик процесса загрузки
public event ProgressEventHandler ProcessProgress;
public event EventHandler RequestStart; // перед загрузкой
public event EventHandler RequestEnd; // после загрузки
public delegate void RedirectHandler(object sender, RedirectEventArgs e);
public event RedirectHandler Redirect;
protected virtual void OnRedirect(object sender , RedirectEventArgs e)
{
if (Redirect != null)
{
Redirect (this, e);
}
}
private void DoRedirect(HttpWebRequest httpWebRequest, HttpWebResponse httpWebResponse, ref bool Handled, ref string path)
{
RedirectEventArgs e = new RedirectEventArgs(httpWebRequest, httpWebResponse, path);
OnRedirect(this, e);
Handled = e.Handled;
path = e.Path;
}
protected virtual void OnRequestStart (EventArgs e)
{
if (RequestStart != null)
{
RequestStart (this, e);
}
}
protected virtual void OnRequestEnd (EventArgs e)
{
if (RequestEnd != null)
{
RequestEnd (this, e);
}
}
protected virtual void OnProcessProgress(DownloadEventArgs e)
{
if (ProcessProgress != null)
{
ProcessProgress.Invoke(this, e);
}
}
public void DoProgress(long bytesRead, long bytesAll, ref bool IsInterrupted , string what)
{
DownloadEventArgs de = new DownloadEventArgs(bytesRead, bytesAll, what);
OnProcessProgress(de);
IsInterrupted = de.IsInterrupted;
}
private void StartRequest()
{
OnRequestStart(null);
}
private void EndRequest()
{
OnRequestEnd (null);
}
#endregion
#region Credencials
private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
private void SetCredencials (HttpWebRequest httpWebRequest)
{
X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
X509Certificate2Collection scollection = (X509Certificate2Collection)store.Certificates.Find(X509FindType.FindByTimeValid, DateTime.Now, false);
foreach (X509Certificate2 x509 in scollection)
{
httpWebRequest.ClientCertificates.Add(x509);
}
httpWebRequest.Credentials = CredentialCache.DefaultCredentials;
ServicePointManager.ServerCertificateValidationCallback +=
new System.Net.Security.RemoteCertificateValidationCallback (CheckValidationResult);
}
// установка proxy в формате host:port:user:pass или host:port
private void SetProxy(HttpWebRequest request)
{
string[] proxyparams = sProxy.Split(':');
try
{
WebProxy proxy = new WebProxy(proxyparams[0], int.Parse(proxyparams[1]));
if (proxyparams.Length == 4)
{
NetworkCredential nc = new NetworkCredential();
nc.UserName = proxyparams[2];
nc.Password = proxyparams[3];
proxy.Credentials = nc;
}
request.Proxy = proxy;
}
catch (IndexOutOfRangeException)
{
throw;
}
}
#endregion
public void ClearCookies ()
{
cookies = new CookieCollection();
RequestHeaders["Cookie"] = string.Empty;
SetCookies.Clear();
}
}
}