/// <summary> /// Performs a POST request with the body consisting of a series of key-value arguments.</summary> /// <param name="url"> /// The URL of the request. If the URL does not begin with <c>http://</c> or <c>https://</c>, it is automatically /// prepended with <see cref="RootUrl"/>.</param> /// <param name="args"> /// The arguments to pass with the POST request. Null values are allowed and ignored.</param> /// <returns> /// The response received from the server.</returns> /// <remarks> /// This method automatically calls either <see cref="PostFormdata"/> or <see cref="PostUrlencoded"/> depending on /// whether the provided arguments contain any file uploads or not.</remarks> public HResponse Post(string url, params HArg[] args) { if (args == null) { args = new HArg[0]; } return(args.Where(a => a != null).All(a => a.ValidForUrlEncoded) ? PostUrlencoded(url, args) : PostFormdata(url, args)); }
/// <summary> /// Performs a POST request with the body consisting of a series of key-value arguments, encoded in the /// <c>application/x-www-form-urlencoded</c> format.</summary> /// <param name="url"> /// The URL of the request. If the URL does not begin with <c>http://</c> or <c>https://</c>, it is automatically /// prepended with <see cref="RootUrl"/>.</param> /// <param name="args"> /// The arguments to pass with the POST request. Null values are allowed and ignored.</param> /// <returns> /// The response received from the server.</returns> /// <remarks> /// Choose this format for requests that need to be small and do not contain any file uploads.</remarks> /// <seealso cref="Post(string,HArg[])"/> public HResponse PostUrlencoded(string url, params HArg[] args) { if (args == null) { args = new HArg[0]; } var invalid = args.Where(a => a != null).FirstOrDefault(a => !a.ValidForUrlEncoded); if (invalid != null) { throw new ArgumentException("args", "The argument with name '{0}' is not valid for URL-encoded POST requests.".Fmt(invalid.Name)); } return(Post(url, args.Where(a => a != null).Select(a => a.Name.UrlEscape() + "=" + a.Value.UrlEscape()).JoinString("&").ToUtf8(), "application/x-www-form-urlencoded")); }
/// <summary> /// Performs a GET request to the specified URL and with the specified query parameters.</summary> /// <param name="url"> /// The URL of the request. If the URL does not begin with <c>http://</c> or <c>https://</c>, it is automatically /// prepended with <see cref="RootUrl"/>.</param> /// <param name="args"> /// Query parameters to add to the end of the URL in the usual <c>?k1=v1&k2=v2&...</c> format. Null values /// are allowed and ignored.</param> /// <returns> /// The response received from the server.</returns> public HResponse Get(string url, params HArg[] args) { if (args == null) { args = new HArg[0]; } if (!args.Where(a => a != null).All(a => a.ValidForUrlEncoded)) { throw new ArgumentException(); } var request = makeRequest(url + (args.Where(a => a != null).Any() ? (url.Contains('?') ? "&" : "?") : "") + args.Where(a => a != null).Select(a => a.Name.UrlEscape() + "=" + a.Value.UrlEscape()).JoinString("&")); request.Method = "GET"; return(performRequest(request)); }
/// <summary> /// Performs a POST request with the body consisting of a series of key-value arguments, encoded in the /// <c>multipart/form-data</c> format.</summary> /// <param name="url"> /// The URL of the request. If the URL does not begin with <c>http://</c> or <c>https://</c>, it is automatically /// prepended with <see cref="RootUrl"/>.</param> /// <param name="args"> /// The arguments to pass with the POST request. Null values are allowed and ignored.</param> /// <returns> /// The response received from the server.</returns> /// <remarks> /// Choose this format for requests that need to contain file uploads.</remarks> /// <seealso cref="Post(string,HArg[])"/> public HResponse PostFormdata(string url, params HArg[] args) { if (args == null) { args = new HArg[0]; } var invalid = args.Where(a => a != null).FirstOrDefault(a => !a.Valid); if (invalid != null) { throw new ArgumentException("args", "The argument with name '{0}' is not valid.".Fmt(invalid.Name)); } string boundary = Rnd.NextBytes(20).ToHex(); var ms = new MemoryStream(300 + args.Where(a => a != null).Sum(a => 30 + a.Name.Length + (a.FileContent == null ? a.Value.Length : a.FileContent.Length))); var sw = new StreamWriter(ms); sw.AutoFlush = true; foreach (var arg in args.Where(a => a != null)) { if (arg.FileContent == null) { sw.WriteLine("--" + boundary); sw.WriteLine(@"Content-Disposition: form-data; name=""" + arg.Name + @""""); sw.WriteLine(); sw.WriteLine(arg.Value); } else { sw.WriteLine("--" + boundary); sw.WriteLine(@"Content-Disposition: form-data; name=""" + arg.Name + @"""; filename=""" + arg.FileName + @""""); sw.WriteLine(@"Content-Type: " + arg.FileContentType); sw.WriteLine(); ms.Write(arg.FileContent); sw.WriteLine(); } } sw.WriteLine("--" + boundary + "--"); return(Post(url, ms.ToArray(), "multipart/form-data; boundary=" + boundary)); }
/// <summary> /// Performs a POST request with the body consisting of a series of key-value arguments, encoded in the /// <c>multipart/form-data</c> format.</summary> /// <param name="url"> /// The URL of the request. If the URL does not begin with <c>http://</c> or <c>https://</c>, it is automatically /// prepended with <see cref="RootUrl"/>.</param> /// <param name="args"> /// The arguments to pass with the POST request. Null values are allowed and ignored.</param> /// <returns> /// The response received from the server.</returns> /// <remarks> /// Choose this format for requests that need to contain file uploads.</remarks> /// <seealso cref="Post(string,HArg[])"/> public HResponse PostFormdata(string url, params HArg[] args) { if (args == null) args = new HArg[0]; var invalid = args.Where(a => a != null).FirstOrDefault(a => !a.Valid); if (invalid != null) throw new ArgumentException("args", "The argument with name '{0}' is not valid.".Fmt(invalid.Name)); string boundary = Rnd.NextBytes(20).ToHex(); var ms = new MemoryStream(300 + args.Where(a => a != null).Sum(a => 30 + a.Name.Length + (a.FileContent == null ? a.Value.Length : a.FileContent.Length))); var sw = new StreamWriter(ms); sw.AutoFlush = true; foreach (var arg in args.Where(a => a != null)) { if (arg.FileContent == null) { sw.WriteLine("--" + boundary); sw.WriteLine(@"Content-Disposition: form-data; name=""" + arg.Name + @""""); sw.WriteLine(); sw.WriteLine(arg.Value); } else { sw.WriteLine("--" + boundary); sw.WriteLine(@"Content-Disposition: form-data; name=""" + arg.Name + @"""; filename=""" + arg.FileName + @""""); sw.WriteLine(@"Content-Type: " + arg.FileContentType); sw.WriteLine(); ms.Write(arg.FileContent); sw.WriteLine(); } } sw.WriteLine("--" + boundary + "--"); return Post(url, ms.ToArray(), "multipart/form-data; boundary=" + boundary); }
/// <summary> /// Performs a POST request with the body consisting of a series of key-value arguments, encoded in the /// <c>application/x-www-form-urlencoded</c> format.</summary> /// <param name="url"> /// The URL of the request. If the URL does not begin with <c>http://</c> or <c>https://</c>, it is automatically /// prepended with <see cref="RootUrl"/>.</param> /// <param name="args"> /// The arguments to pass with the POST request. Null values are allowed and ignored.</param> /// <returns> /// The response received from the server.</returns> /// <remarks> /// Choose this format for requests that need to be small and do not contain any file uploads.</remarks> /// <seealso cref="Post(string,HArg[])"/> public HResponse PostUrlencoded(string url, params HArg[] args) { if (args == null) args = new HArg[0]; var invalid = args.Where(a => a != null).FirstOrDefault(a => !a.ValidForUrlEncoded); if (invalid != null) throw new ArgumentException("args", "The argument with name '{0}' is not valid for URL-encoded POST requests.".Fmt(invalid.Name)); return Post(url, args.Where(a => a != null).Select(a => a.Name.UrlEscape() + "=" + a.Value.UrlEscape()).JoinString("&").ToUtf8(), "application/x-www-form-urlencoded"); }
/// <summary> /// Performs a POST request with the body consisting of a series of key-value arguments.</summary> /// <param name="url"> /// The URL of the request. If the URL does not begin with <c>http://</c> or <c>https://</c>, it is automatically /// prepended with <see cref="RootUrl"/>.</param> /// <param name="args"> /// The arguments to pass with the POST request. Null values are allowed and ignored.</param> /// <returns> /// The response received from the server.</returns> /// <remarks> /// This method automatically calls either <see cref="PostFormdata"/> or <see cref="PostUrlencoded"/> depending on /// whether the provided arguments contain any file uploads or not.</remarks> public HResponse Post(string url, params HArg[] args) { if (args == null) args = new HArg[0]; return args.Where(a => a != null).All(a => a.ValidForUrlEncoded) ? PostUrlencoded(url, args) : PostFormdata(url, args); }
/// <summary> /// Performs a GET request to the specified URL and with the specified query parameters.</summary> /// <param name="url"> /// The URL of the request. If the URL does not begin with <c>http://</c> or <c>https://</c>, it is automatically /// prepended with <see cref="RootUrl"/>.</param> /// <param name="args"> /// Query parameters to add to the end of the URL in the usual <c>?k1=v1&k2=v2&...</c> format. Null values /// are allowed and ignored.</param> /// <returns> /// The response received from the server.</returns> public HResponse Get(string url, params HArg[] args) { if (args == null) args = new HArg[0]; if (!args.Where(a => a != null).All(a => a.ValidForUrlEncoded)) throw new ArgumentException(); var request = makeRequest(url + (args.Where(a => a != null).Any() ? (url.Contains('?') ? "&" : "?") : "") + args.Where(a => a != null).Select(a => a.Name.UrlEscape() + "=" + a.Value.UrlEscape()).JoinString("&")); request.Method = "GET"; return performRequest(request); }