/// <summary> /// 下载文件。 /// </summary> /// <param name="address">网址。</param> /// <param name="fileName">本地保存文件位置。</param> /// <param name="refererUrl">来路(引用页面)。</param> public void DownloadFile(Uri address, string fileName, Uri refererUrl) { lock (this) { using (HttpWebClient webclient = CreateWebClient(refererUrl)) { DownloadFileInternal(address, fileName, webclient, 0, null); ResponseHeaders = webclient.ResponseHeaders; } } }
/// <summary> /// 上传数据(自定义上传方式)。 /// </summary> /// <param name="address">网址。</param> /// <param name="data">要上传的数据。</param> /// <param name="refererUrl">来路(引用页面)。</param> /// <returns>返回上传后获取到的数据。</returns> public byte[] UploadData(Uri address, byte[] data, Uri refererUrl) { lock (this) { using (HttpWebClient webclient = CreateWebClient(refererUrl)) { byte[] result = UploadDataInternal(address, data, webclient, 0, null); ResponseHeaders = webclient.ResponseHeaders; return(result); } } }
/// <summary> /// 上传文件。 /// </summary> /// <param name="address">网址。</param> /// <param name="fileName">本地文件位置。</param> /// <param name="refererUrl">来路(引用页面)。</param> /// <returns>返回上传之后获取到的数据。</returns> public byte[] UploadFile(Uri address, string fileName, Uri refererUrl) { lock (this) { using (HttpWebClient webclient = CreateWebClient(refererUrl)) { byte[] result = UploadFileInternal(address, fileName, webclient, 0, null); ResponseHeaders = webclient.ResponseHeaders; return(result); } } }
/// <summary> /// 上传数据(Post文本,普通表单)。 /// </summary> /// <param name="address">网址。</param> /// <param name="data">要上传的数据。</param> /// <param name="refererUrl">来路(引用页面)。</param> /// <returns>返回上传后获取到的内容。</returns> public string UploadString(Uri address, string data, Uri refererUrl) { lock (this) { using (HttpWebClient webclient = CreateWebClient(refererUrl)) { string result = UploadStringInternal(address, data, webclient, 0, null); ResponseHeaders = webclient.ResponseHeaders; return(result); } } }
/// <summary> /// 创建HttpWebClient的实例。 /// </summary> /// <param name="refererUrl">来路(引用页面)。</param> /// <returns>返回HttpWebClient的实例。</returns> protected HttpWebClient CreateWebClient(Uri refererUrl) { HttpWebClient result = new HttpWebClient(); //result.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; CIBA; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)"; //result.Headers["User-Agent"] = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)"; //result.Headers["Accept"] = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; //result.Headers["Accept-Language"] = "zh-cn"; result.BaseAddress = BaseAddress; if (Headers != null) { foreach (string item in Headers.AllKeys) { result.Headers[item] = Headers[item]; } } if (refererUrl != null) { result.Headers["Referer"] = refererUrl.AbsoluteUri; } if (AutomaticDecompression != null) { result.AutomaticDecompression = AutomaticDecompression.Value; } if (AllowAutoRedirect != null) { result.AllowAutoRedirect = AllowAutoRedirect.Value; } if (Expect100Continue != null) { result.Expect100Continue = Expect100Continue.Value; } if (AutoAppendPostHeader != null) { result.AutoAppendPostHeader = AutoAppendPostHeader.Value; } if (Timeout > 0) { result.Timeout = Timeout; } result.Cookies = Cookies; if (Proxy != null) { result.Proxy = Proxy; } if (Encoding != null) { result.Encoding = Encoding; } return(result); }
private byte[] UploadDataInternal(Uri address, byte[] data, HttpWebClient webclient, int count, Exception e) { if (count > RetryCount) { if (e != null) { throw e; } return(new byte[0]); } try { return(webclient.UploadData(address, data)); } catch (WebException ex) { return(UploadDataInternal(address, data, webclient, count + 1, ex)); } }
private byte[] UploadFileInternal(Uri address, string fileName, HttpWebClient webclient, int count, Exception e) { if (count > RetryCount) { if (e != null) { throw e; } return(new byte[0]); } try { return(webclient.UploadFile(address, fileName)); } catch (WebException ex) { return(UploadFileInternal(address, fileName, webclient, count + 1, ex)); } }
private string UploadStringInternal(Uri address, string data, HttpWebClient webclient, int count, Exception e) { if (count > RetryCount) { if (e != null) { throw e; } return(string.Empty); } try { return(webclient.UploadString(address, data)); } catch (WebException ex) { return(UploadStringInternal(address, data, webclient, count + 1, ex)); } }
private void DownloadFileInternal(Uri address, string fileName, HttpWebClient webclient, int count, Exception e) { if (count > RetryCount) { if (e != null) { throw e; } return; } try { webclient.DownloadFile(address, fileName); } catch (WebException ex) { DownloadFileInternal(address, fileName, webclient, count + 1, ex); } }