如何在正文请求中将参数发送到Web API?(How to send parameters to web API in the body request?)
作为集成测试的一部分,我试图从控制台应用程序中使用WebAPI。 我使用这种方法:
private static string Consume(string endpoint, string user, string password) { var client = new HttpClient(); client.BaseAddress = new Uri(endpoint); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); string body = "{\"User\":\"" + user + "\",\"Password\":\"" + password + "\"}"; var response = client.PostAsync("api/ticket", new StringContent(body, Encoding.UTF8, "application/json")).Result; response.EnsureSuccessStatusCode(); if (response.IsSuccessStatusCode) ... //process return string.Empty; }我对将参数发送到WebAPI方法的方式感到不满意,我更喜欢使用类型化的对象,如:
public class Credentials { public string User { get; set; } public string Password { get; set; } }并放在体内。 有没有人有这方面的建议?
I am trying to consume a WebAPI from a console application, as part of an integration test. I use this method:
private static string Consume(string endpoint, string user, string password) { var client = new HttpClient(); client.BaseAddress = new Uri(endpoint); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); string body = "{\"User\":\"" + user + "\",\"Password\":\"" + password + "\"}"; var response = client.PostAsync("api/ticket", new StringContent(body, Encoding.UTF8, "application/json")).Result; response.EnsureSuccessStatusCode(); if (response.IsSuccessStatusCode) ... //process return string.Empty; }I don't feel very satisfied with the way I am sending the parameters to the WebAPI method, I prefer to use a typed object like:
public class Credentials { public string User { get; set; } public string Password { get; set; } }and put it in the body. Does anyone has a suggestion for this?
最满意答案
使用Newtonsoft.Json库来序列化您的凭证对象。 你的Consume功能就是这样
private static string Consume(string endpoint, string user, string password) { var client = new HttpClient(); client.BaseAddress = new Uri(endpoint); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); var credential = new Credentials { User = user, Password = password }; var credentialString = Newtonsoft.Json.JsonConvert.SerializeObject(credential, Formatting.None); var response = client.PostAsync("api/ticket", credentialString).Result; response.EnsureSuccessStatusCode(); if (response.IsSuccessStatusCode) ... //process return string.Empty; }credentialString是序列化的Credentials类对象的字符串。
Use Newtonsoft.Json library to serialize your credential object. Your Consume function would be like this
private static string Consume(string endpoint, string user, string password) { var client = new HttpClient(); client.BaseAddress = new Uri(endpoint); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); var credential = new Credentials { User = user, Password = password }; var credentialString = Newtonsoft.Json.JsonConvert.SerializeObject(credential, Formatting.None); var response = client.PostAsync("api/ticket", credentialString).Result; response.EnsureSuccessStatusCode(); if (response.IsSuccessStatusCode) ... //process return string.Empty; }credentialString is serialize string of Credentials class object.
更多推荐
发布评论