public static string HTTPFetch(string url, string method, WebHeaderCollection headers, string payload, string contentType = "application/x-www-form-urlencoded") { HttpWebRequest request = (HttpWebRequest)WebRequest.CreateHttp(url); if (Proxy != null) { request.Proxy = Proxy; } request.Headers = headers; request.Method = method; request.Accept = "application/vnd.vimeo.*+json; version=3.2"; request.ContentType = contentType; request.KeepAlive = false; //ServicePointManager.Expect100Continue = true; //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; //ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; if (!String.IsNullOrWhiteSpace(payload)) { var streamBytes = Helpers.ToByteArray(payload); request.ContentLength = streamBytes.Length; Stream reqStream = request.GetRequestStream(); reqStream.Write(streamBytes, 0, streamBytes.Length); reqStream.Close(); } HttpWebResponse response = (HttpWebResponse)(request.GetResponse()); Debug.WriteLine(((HttpWebResponse)response).StatusDescription); var dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); Debug.WriteLine(String.Format("Response from URL {0}:", url), "HTTPFetch"); Debug.WriteLine(responseFromServer, "HTTPFetch"); return(responseFromServer); }
public static async Task <string> HTTPFetchAsync(string url, string method, WebHeaderCollection headers, string payload, string contentType = "application/x-www-form-urlencoded") { HttpWebRequest request = (HttpWebRequest)WebRequest.CreateHttp(url); if (Proxy != null) { request.Proxy = Proxy; } request.Headers = headers; request.Method = method; request.Accept = "application/vnd.vimeo.*+json; version=3.2"; request.ContentType = contentType; request.KeepAlive = false; var streamBytes = Helpers.ToByteArray(payload); request.ContentLength = streamBytes.Length; Stream dataStream = await request.GetRequestStreamAsync(); await dataStream.WriteAsync(streamBytes, 0, streamBytes.Length); dataStream.Close(); HttpWebResponse response = (HttpWebResponse)(await request.GetResponseAsync()); Debug.WriteLine(((HttpWebResponse)response).StatusDescription); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromServer = reader.ReadToEnd(); reader.Close(); dataStream.Close(); response.Close(); Debug.WriteLine(String.Format("Response from URL {0}:", url), "HTTPFetch"); Debug.WriteLine(responseFromServer, "HTTPFetch"); return(responseFromServer); }