HttpClient(C#)
Web系を開発していると動作をトレースするためにHttpクライアントが必要な場合があります。
using System; using System.Collections; using System.Collections.Generic; using System.Net; using System.Net.Http; using System.IO; using System.Text; using System.Threading.Tasks; using System.Web; namespace HttpClientSim { class HttpClientSim { public static HttpResponseMessage PostJson(string url, Dictionary<string, string> headers, Dictionary<string, string> querys,string json, ProxyInfo info) { HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, url); // request header foreach (KeyValuePair<string, string> pair in headers) { req.Headers.Add(pair.Key, pair.Value); } // query string Uri uri = null; if (querys != null) { var query = HttpUtility.ParseQueryString(""); foreach (KeyValuePair<string, string> pair in querys) { query.Add(pair.Key, pair.Value); } var builder = new UriBuilder(url); builder.Query = query.ToString(); uri = builder.Uri; } else { var builder = new UriBuilder(url); uri = builder.Uri; } // json content if (json == null) json = ""; StringContent jsonContent = new StringContent(json, Encoding.UTF8, "application/json"); using (HttpClientHandler handler = new HttpClientHandler()) { // no verify handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }; // proxy if (info != null) { handler.Proxy = new WebProxy(info.Url); handler.Credentials = new NetworkCredential(info.Id, info.Pass); handler.UseProxy = true; } // request using (HttpClient client = new HttpClient(handler)) { Tasktask = client.PostAsync(uri, jsonContent); return task.Result; } } } public static HttpResponseMessage Post(string url, Dictionary<string, string> headers, Dictionary<string, string> querys, Dictionary<string, string> bodys, ProxyInfo info) { HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, url); // request header foreach (KeyValuePair<string, string> pair in headers) { req.Headers.Add(pair.Key, pair.Value); } // query string Uri uri = null; if (querys != null) { var query = HttpUtility.ParseQueryString(""); foreach (KeyValuePair<string, string> pair in querys) { query.Add(pair.Key, pair.Value); } var builder = new UriBuilder(url); builder.Query = query.ToString(); uri = builder.Uri; } else { var builder = new UriBuilder(url); uri = builder.Uri; } // body content if (bodys == null) bodys = new Dictionary<string, string>(); var formContent = new FormUrlEncodedContent(bodys); using (HttpClientHandler handler = new HttpClientHandler()) { // no verify handler.ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => { return true; }; // proxy if (info != null) { handler.Proxy = new WebProxy(info.Url); handler.Credentials = new NetworkCredential(info.Id, info.Pass); handler.UseProxy = true; } // request using (HttpClient client = new HttpClient(handler)) { Task task = client.PostAsync(uri, formContent); return task.Result; } } } } class ProxyInfo { public string Url { get; } public string Id { get; } public string Pass { get; } public ProxyInfo(string url,string id,string pass) { if(url == null || id == null || pass == null) { throw new ArgumentException("引数不正"); } this.Url = url; this.Id = id; this.Pass = pass; } } class Program { static void Main(string[] args) { string url = "http://localhost:3000/aa"; var bodys = "{\"aa\":\"bbbbb\",\"dddsa\":\"deee\"}"; var qs = new Dictionary<string, string>(); qs.Add("ffff", "ggg g"); var res = HttpClientSim.PostJson(url, new Dictionary<string, string>(), qs, bodys, null); Console.WriteLine(res.StatusCode); } } }