ASP.NET API接口使用HttpClient进行测试和验证,以保证我们的接口的可用性。
参考Github开源项目:https://github.com/MikeWasson/HttpClientSample
namespace HttpClientSample
{
public class Product
{
public string Id { get; set; }
public string Name { get; set; }
public decimal Price { get; set; }
public string Category { get; set; }
}
}
using ProductApi.Models;
using System;
using System.Collections.Concurrent;
using System.Net;
using System.Web.Http;
using System.Web.Http.Results;
namespace ProductApi.Controllers
{
[RoutePrefix("api/products")]
public class ProductsController : ApiController
{
private static ConcurrentDictionary _products = new ConcurrentDictionary();
[Route("{id}", Name = "GetById")]
public IHttpActionResult Get(string id)
{
Product product = null;
if (_products.TryGetValue(id, out product))
{
return Ok(product);
}
else
{
return NotFound();
}
}
[HttpPost]
[Route("")]
public IHttpActionResult Post(Product product)
{
if (product == null)
{
return BadRequest("Product cannot be null");
}
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
product.Id = Guid.NewGuid().ToString();
_products[product.Id] = product;
return CreatedAtRoute("GetById", new { id = product.Id }, product);
}
[HttpPut]
[Route("{id}")]
public IHttpActionResult Put(string id, Product product)
{
if (product == null)
{
return BadRequest("Product cannot be null");
}
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (product.Id != id)
{
return BadRequest("product.id does not match id parameter");
}
if (!_products.Keys.Contains(id))
{
return NotFound();
}
_products[id] = product;
return new StatusCodeResult(HttpStatusCode.NoContent, this);
}
[HttpDelete]
[Route("{id}")]
public IHttpActionResult Delete(string id)
{
Product product = null;
_products.TryRemove(id, out product);
return new StatusCodeResult(HttpStatusCode.NoContent, this);
}
}
}
using System;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace HttpClientSample
{
class Program
{
static HttpClient client = new HttpClient();
static void ShowProduct(Product product)
{
Console.WriteLine($"Name: {product.Name}\tPrice: {product.Price}\tCategory: {product.Category}");
}
static async Task CreateProductAsync(Product product)
{
HttpResponseMessage response = await client.PostAsJsonAsync("api/products", product);
response.EnsureSuccessStatusCode();
// return URI of the created resource.
return response.Headers.Location;
}
static async Task GetProductAsync(string path)
{
Product product = null;
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
product = await response.Content.ReadAsAsync();
}
return product;
}
static async Task UpdateProductAsync(Product product)
{
HttpResponseMessage response = await client.PutAsJsonAsync($"api/products/{product.Id}", product);
response.EnsureSuccessStatusCode();
// Deserialize the updated product from the response body.
product = await response.Content.ReadAsAsync();
return product;
}
static async Task DeleteProductAsync(string id)
{
HttpResponseMessage response = await client.DeleteAsync($"api/products/{id}");
return response.StatusCode;
}
static void Main()
{
RunAsync().Wait();
}
static async Task RunAsync()
{
client.BaseAddress = new Uri("http://localhost:55268/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
try
{
// Create a new product
Product product = new Product { Name = "Gizmo", Price = 100, Category = "Widgets" };
var url = await CreateProductAsync(product);
Console.WriteLine($"Created at {url}");
// Get the product
product = await GetProductAsync(url.PathAndQuery);
ShowProduct(product);
// Update the product
Console.WriteLine("Updating price...");
product.Price = 80;
await UpdateProductAsync(product);
// Get the updated product
product = await GetProductAsync(url.PathAndQuery);
ShowProduct(product);
// Delete the product
var statusCode = await DeleteProductAsync(product.Id);
Console.WriteLine($"Deleted (HTTP Status = {(int)statusCode})");
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
}
}
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml.Serialization;
namespace RTVSApi.Utils
{
///
/// HttpClient工具类
///
public class HttpClientUtil
{
///
/// post请求
///
///
/// post数据
///
public static string PostResponse(string url, string postData)
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpContent httpContent = new StringContent(postData);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
if (response.IsSuccessStatusCode)
{
string result = response.Content.ReadAsStringAsync().Result;
return result;
}
return null;
}
///
/// 发起post请求
///
///
/// url
/// post数据
///
public static string PostResponse(string url, T postData) where T : class, new()
{
string result = "0";
var format = new IsoDateTimeConverter();
format.DateTimeFormat = "yyyyMMddHHmmssSSS";
var dataJson = JsonConvert.SerializeObject(postData, Newtonsoft.Json.Formatting.Indented, format);
var content = new StringContent(dataJson, Encoding.UTF8, "application/json");
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = httpClient.PostAsync(url, content).Result;
if (response.IsSuccessStatusCode)
{
//Task t = response.Content.ReadAsStringAsync();
//string s = t.Result;
//T ss = JsonConvert.DeserializeObject(s);
result = "1";
}
return result;
}
///
/// 发起post请求主键返回
///
///
/// url
/// post数据
/// 主键
public static string PostResponseKey(string url, T postData) where T : class, new()
{
string ret = "0";
var format = new IsoDateTimeConverter();
format.DateTimeFormat = "yyyyMMddHHmmssSSS";
var dataJson = JsonConvert.SerializeObject(postData, Newtonsoft.Json.Formatting.Indented, format);
var content = new StringContent(dataJson, Encoding.UTF8, "application/json");
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = httpClient.PostAsync(url, content).Result;
if (response.IsSuccessStatusCode)
{
Task t = response.Content.ReadAsStringAsync();
ret = t.Result;
}
return ret;
}
///
/// 发起post请求
///
///
/// url
/// post数据
///
public static T PostResponse(string url, string postData) where T : class, new()
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpContent httpContent = new StringContent(postData);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpClient httpClient = new HttpClient();
T result = default(T);
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
if (response.IsSuccessStatusCode)
{
Task t = response.Content.ReadAsStringAsync();
string s = t.Result;
result = JsonConvert.DeserializeObject(s);
}
return result;
}
///
/// V3接口全部为Xml形式,故有此方法
///
///
///
///
///
public static T PostXmlResponse(string url, string xmlString) where T : class, new()
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpContent httpContent = new StringContent(xmlString);
httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpClient httpClient = new HttpClient();
T result = default(T);
HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
if (response.IsSuccessStatusCode)
{
Task t = response.Content.ReadAsStringAsync();
string s = t.Result;
result = XmlDeserialize(s);
}
return result;
}
///
/// 反序列化Xml
///
///
///
///
public static T XmlDeserialize(string xmlString) where T : class, new()
{
try
{
XmlSerializer ser = new XmlSerializer(typeof(T));
using (StringReader reader = new StringReader(xmlString))
{
return (T)ser.Deserialize(reader);
}
}
catch (Exception ex)
{
throw new Exception("XmlDeserialize发生异常:xmlString:" + xmlString + "异常信息:" + ex.Message);
}
}
///
/// get请求
///
///
///
public static string GetResponse(string url)
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = httpClient.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
string result = response.Content.ReadAsStringAsync().Result;
return result;
}
return null;
}
///
/// get请求
///
///
///
///
public static T GetResponse(string url) where T : class, new()
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpClient httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromMilliseconds(3000);
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = httpClient.GetAsync(url).Result;
T result = default(T);
if (response.IsSuccessStatusCode)
{
Task t = response.Content.ReadAsStringAsync();
string s = t.Result;
result = JsonConvert.DeserializeObject(s);
}
return result;
}
///
/// Get请求返回List集合
///
///
///
/// List
public static List GetResponseList(string url) where T : class, new()
{
if (url.StartsWith("https"))
{
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
}
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = httpClient.GetAsync(url).Result;
List result = default(List);
if (response.IsSuccessStatusCode)
{
Task t = response.Content.ReadAsStringAsync();
string s = t.Result;
if (s != null && !s.StartsWith("["))
{
s = "[" + s + "]";
}
result = JsonConvert.DeserializeObject>(s);
}
return result;
}
///
/// Delete请求
///
///
/// post数据
///
public static string DeleteResponse(string url)
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = httpClient.DeleteAsync(url).Result;
if (response.IsSuccessStatusCode)
{
string result = response.Content.ReadAsStringAsync().Result;
return "1";
}
return "0";
}
///
/// 发起put请求 List格式数据
///
///
/// url
/// put数据
///
public static string PutListDataResponse(string url, List postData) where T : class, new()
{
string result = "0";
if (postData != null && postData.Count > 0)
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpClient httpClient = new HttpClient();
var format = new IsoDateTimeConverter();
format.DateTimeFormat = "yyyyMMddHHmmssSSS";
var dataJson = JsonConvert.SerializeObject(postData[0], Newtonsoft.Json.Formatting.Indented, format);
var content = new StringContent(dataJson, Encoding.UTF8, "application/json");
HttpResponseMessage response = httpClient.PutAsync(url, content).Result;
if (response.IsSuccessStatusCode)
{
result = "1";
}
}
else
{
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = httpClient.PutAsync(url, null).Result;
if (response.IsSuccessStatusCode)
{
result = "1";
}
}
return result;
}
///
/// 发起put请求 (修改时候用)
///
///
/// url
/// put数据
///
public static string PutDataResponse(string url, T postData) where T : class, new()
{
string result = "0";
if (postData != null)
{
if (url.StartsWith("https"))
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls;
HttpClient httpClient = new HttpClient();
var format = new IsoDateTimeConverter();
format.DateTimeFormat = "yyyyMMddHHmmssSSS";
var dataJson = JsonConvert.SerializeObject(postData, Newtonsoft.Json.Formatting.Indented, format);
var content = new StringContent(dataJson, Encoding.UTF8, "application/json");
HttpResponseMessage response = httpClient.PutAsync(url, content).Result;
if (response.IsSuccessStatusCode)
{
result = "1";
}
}
else
{
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = httpClient.PutAsync(url, null).Result;
if (response.IsSuccessStatusCode)
{
result = "1";
}
}
return result;
}
}
}