/// <summary> /// 从指定的地址获取包装为JSON对象的响应结果。 /// </summary> /// <param name="url"></param> /// <param name="method"></param> /// <param name="data"></param> /// <returns></returns> private static async Task <IResult> Execute(string url, string method, IDictionary <string, object> data = null) { var uri = NormalUri(url); method = Commons.NotBlank(method, "method").Trim().ToUpper(); using (var client = new HttpWebClient(m_cookieContainer)) { try { client.Headers.Set("Accept", "application/json"); Log.DebugFormat("sending data to: {0}", uri); m_status = HttpStatus.Sending; client.UploadProgressChanged += client_UploadProgressChanged; client.UploadValuesCompleted += client_UploadValuesCompleted; client.DownloadProgressChanged += client_DownloadProgressChanged; client.DownloadDataCompleted += client_DownloadDataCompleted; var content = Encoding.UTF8.GetString( await client.UploadValuesTaskAsync(uri, method, ToNameValueCollection(data))); Log.DebugFormat("received data: {0}", content); return(ToResult(content)); } catch (WebException ex) { return(new JsonResult(new RpcException(ex.Response, ex))); } finally { m_status = HttpStatus.Ready; m_progress = 0; } } }
/// <summary> /// 从指定的地址获取包装为JSON对象的响应结果。 /// </summary> /// <param name="url"></param> /// <param name="data"></param> /// <returns></returns> public static async Task <IResult> Get(string url, IDictionary <string, object> data = null) { var uri = NormalUri(url); using (var client = new HttpWebClient(m_cookieContainer)) { m_status = HttpStatus.Receiving; client.UploadProgressChanged += client_UploadProgressChanged; client.UploadValuesCompleted += client_UploadValuesCompleted; client.DownloadProgressChanged += client_DownloadProgressChanged; client.DownloadDataCompleted += client_DownloadDataCompleted; client.Headers.Set("Accept", "application/json"); client.QueryString = UrlEncode(ToNameValueCollection(data)); if (client.QueryString != null) { Log.DebugFormat("params = {{{0}}}", string.Join(", ", client.QueryString.AllKeys .Select(_ => string.Format("{0}:{1}", _, client.QueryString[_])))); } Log.DebugFormat("receiving data from: {0}", uri); try { var content = Encoding.UTF8.GetString(await client.DownloadDataTaskAsync(uri)); return(ToResult(content)); } catch (WebException ex) { return(new JsonResult(new RpcException(ex.Response, ex))); } finally { m_status = HttpStatus.Ready; m_progress = 0; } } }
/// <summary> /// 获取指定地址的资源的长度(以字节为单位)。 /// </summary> /// <param name="url">请求地址。</param> /// <returns>资源内容的长度,如果资源不存在则返回0。</returns> public static async Task <long> Head(string url) { var uri = NormalUri(url); using (var client = new HttpWebClient(m_cookieContainer)) { m_status = HttpStatus.Receiving; client.OnlyHead = true; client.UploadProgressChanged += client_UploadProgressChanged; client.UploadValuesCompleted += client_UploadValuesCompleted; client.DownloadProgressChanged += client_DownloadProgressChanged; client.DownloadDataCompleted += client_DownloadDataCompleted; try { await client.DownloadDataTaskAsync(uri); return(client.ContentLength); } catch (WebException) { return(0); } finally { m_status = HttpStatus.Ready; m_progress = 0; } } }
/// <summary> /// 向指定的地址上传数据 /// </summary> /// <param name="url">请求地址。</param> /// <param name="data">参数。</param> /// <returns>上传的响应。</returns> public static async Task <IResult> PostRaw(string url, byte[] data, IDictionary <string, object> d = null) { var uri = NormalUri(url); using (var client = new HttpWebClient(m_cookieContainer)) { m_status = HttpStatus.Receiving; client.UploadProgressChanged += client_UploadProgressChanged; client.UploadValuesCompleted += client_UploadValuesCompleted; client.DownloadProgressChanged += client_DownloadProgressChanged; client.DownloadDataCompleted += client_DownloadDataCompleted; try { var content = Encoding.UTF8.GetString(await client.UploadDataTaskAsync(uri, "POST", data)); return(ToResult(content)); } catch (WebException ex) { return(new JsonResult(new RpcException(ex.Response, ex))); } finally { m_status = HttpStatus.Ready; m_progress = 0; } } }
/// <summary> /// 从指定的地址获取包含原始数据的字节数组。如果参数start和end都是0,那么表示不指定范围。 /// </summary> /// <param name="url">请求地址。</param> /// <param name="start">请求内容的起始偏移(从0开始)。</param> /// <param name="end">请求内容的终止偏移(从0开始,不包含。)</param> /// <returns></returns> public static async Task <byte[]> GetRaw(string url, long start = 0L, long end = 0L) { var uri = NormalUri(url); using (var client = new HttpWebClient(m_cookieContainer)) { m_status = HttpStatus.Receiving; client.AddRange(start, end); client.UploadProgressChanged += client_UploadProgressChanged; client.UploadValuesCompleted += client_UploadValuesCompleted; client.DownloadProgressChanged += client_DownloadProgressChanged; client.DownloadDataCompleted += client_DownloadDataCompleted; try { return(await client.DownloadDataTaskAsync(uri)); } catch (WebException) { return(new byte[0]); } finally { m_status = HttpStatus.Ready; m_progress = 0; } } }