/// <summary> /// <para>Available parameters:</para> /// <para>- <c>MethodType</c> type (required) [ GET, POST ]</para> /// <para>- <c>string</c> url (required)</para> /// <para>- <c>IDictionary</c> param (required)</para> /// <para>- <c>IDictionary</c> header (optional)</para> /// </summary> /// <returns>AsyncResponse.</returns> public async Task <AsyncResponse> SendRequestAsync(MethodType type, string url, IDictionary <string, string> param, IDictionary <string, string> headers = null) { var httpClient = new HttpClient(); httpClient.DefaultRequestHeaders.Add("Referer", "http://spapi.pixiv.net/"); httpClient.DefaultRequestHeaders.Add("User-Agent", "PixivIOSApp/5.8.0"); httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + this.AccessToken); if (headers != null) { foreach (var header in headers) { httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); } } AsyncResponse asyncResponse = null; if (type == MethodType.POST) { var reqParam = new FormUrlEncodedContent(param); var response = await httpClient.PostAsync(url, reqParam); asyncResponse = new AsyncResponse(response); } else if (type == MethodType.DELETE) { var uri = url; if (param != null) { var query_string = ""; foreach (KeyValuePair <string, string> kvp in param) { if (query_string == "") { query_string += "?"; } else { query_string += "&"; } query_string += kvp.Key + "=" + WebUtility.UrlEncode(kvp.Value); } uri += query_string; } var response = await httpClient.DeleteAsync(uri); asyncResponse = new AsyncResponse(response); } else { var uri = url; if (param != null) { var query_string = ""; foreach (KeyValuePair <string, string> kvp in param) { if (query_string == "") { query_string += "?"; } else { query_string += "&"; } query_string += kvp.Key + "=" + WebUtility.UrlEncode(kvp.Value); } uri += query_string; } var response = await httpClient.GetAsync(uri); asyncResponse = new AsyncResponse(response); } return(asyncResponse); }
private static async Task <AsyncResponse> SendRequestWithoutHeaderAsync(MethodType type, string url, IDictionary <string, string> param, IDictionary <string, string> headers, HttpClient httpClient) { if (headers != null) { foreach (var header in headers) { httpClient.DefaultRequestHeaders.Add(header.Key, header.Value); } } AsyncResponse asyncResponse = null; if (type == MethodType.POST) { var reqParam = new FormUrlEncodedContent(param); //reqParam.Headers.ContentType="" var response = await httpClient.PostAsync(url, reqParam); asyncResponse = new AsyncResponse(response); } else if (type == MethodType.DELETE) { var uri = url; if (param != null) { var query_string = ""; foreach (KeyValuePair <string, string> kvp in param) { if (query_string == "") { query_string += "?"; } else { query_string += "&"; } query_string += WebUtility.UrlEncode(kvp.Key) + "=" + WebUtility.UrlEncode(kvp.Value); } uri += query_string; } var response = await httpClient.DeleteAsync(uri); asyncResponse = new AsyncResponse(response); } else { var uri = url; if (param != null) { var query_string = ""; foreach (KeyValuePair <string, string> kvp in param) { if (query_string == "") { query_string += "?"; } else { query_string += "&"; } query_string += kvp.Key + "=" + WebUtility.UrlEncode(kvp.Value); } uri += query_string; } var response = await httpClient.GetAsync(uri); string vl = response.Content.Headers.ContentEncoding.FirstOrDefault(); if (vl != null && vl == "gzip") { asyncResponse = new StreamAsyncResponse(response, new System.IO.Compression.GZipStream(await response.Content.ReadAsStreamAsync(), System.IO.Compression.CompressionMode.Decompress)); } else { asyncResponse = new AsyncResponse(response); } } return(asyncResponse); }