Пример #1
0
        /// <summary>
        /// 开始一个异步任务来获取GET请求内容。
        /// </summary>
        /// <param name="url">请求链接</param>
        /// <returns>一个异步任务对象,任务完成后可通过其 Result 属性获取返回的流内容</returns>
        public static async Task <Stream> GetStreamAsync(string url, Preference preference = null)
        {
            Uri        uri = new Uri(url);
            HttpClient client;

            if (preference != null)
            {
                HttpClientHandler handler = new HttpClientHandler();
                handler.Credentials = new NetworkCredential(preference.Username, preference.Password);
                client = new HttpClient(handler);
            }
            else
            {
                client = _client;
            }
            HttpResponseMessage response = await client.GetAsync(uri);

            Debug.WriteLine("code in repsonse message: {0}, url: {1}", response.StatusCode, url);
            response.EnsureSuccessStatusCode();
            return(await response.Content.ReadAsStreamAsync());
        }
Пример #2
0
        /// <summary>
        /// 开始一个异步任务来获取POST请求内容
        /// </summary>
        /// <param name="url">请求链接</param>
        /// <param name="data">发送内容</param>
        /// <returns>一个异步任务对象,任务完成后可通过其 Result 属性获取返回的流内容</returns>
        public static async Task <String> PostStringAsync(string url, string data, Preference preference = null)
        {
            HttpClient client;

            if (preference != null)
            {
                HttpClientHandler handler = new HttpClientHandler();
                handler.Credentials = new NetworkCredential(preference.Username, preference.Password);
                client = new HttpClient(handler);
            }
            else
            {
                client = _client;
            }
            HttpContent         content  = new StringContent(data, Encoding.UTF8, "application/json");
            HttpResponseMessage response = await client.PostAsync(url, content);

            Debug.WriteLine("code in repsonse message: {0}, url: {1}", response.StatusCode, url);
            response.EnsureSuccessStatusCode();
            return(await response.Content.ReadAsStringAsync());
        }
Пример #3
0
        }                                //同步任何数据前,先查此项

        public PocketAPI()
        {
            preference = new Preference();
            IsSync     = true;
        }