/// <summary> /// Sends a GET request and returns the response body. /// </summary> /// <param name="url">The target url.</param> /// <param name="requestModifier">An action that modifies the request.</param> /// <returns></returns> public static string Get(string url, Action <HttpWebRequest> requestModifier = null) { var request = (HttpWebRequest)WebRequest.Create(url); Compatibility.ModifyRequest(request); RequestModifier?.Invoke(request); requestModifier?.Invoke(request); return(request.GetResponseText()); }
/// <summary> /// Sends a POST request and returns the response body. /// </summary> /// <param name="url">The target url.</param> /// <param name="data">The string data to upload.</param> /// <param name="requestModifier">An action that modifies the request.</param> /// <returns></returns> public static string Post(string url, string data, Action <HttpWebRequest> requestModifier = null) { var bytes = Encoding.UTF8.GetBytes(data); var request = (HttpWebRequest)WebRequest.Create(url); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; Compatibility.ModifyRequest(request); RequestModifier?.Invoke(request); requestModifier?.Invoke(request); using (var stream = Compatibility.GetRequestStream(request)) { stream.Write(bytes, 0, bytes.Length); return(request.GetResponseText()); } }