HttpPut.cs 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using Newtonsoft.Json;
  2. using Newtonsoft.Json.Converters;
  3. using System.Text;
  4. namespace AmrControl.Common.HttpClients
  5. {
  6. public class HttpPut : BaseHttpClient
  7. {
  8. /// <summary>
  9. /// 设置传输类型,请在请求时注意类型,
  10. /// </summary>
  11. private MediaType mediaType = MediaType.APPLICATION_JSON_VALUE;
  12. public HttpPut(HttpClient httpClient, string baseUri) : base(httpClient, baseUri)
  13. {
  14. }
  15. public void setMediaType(MediaType type)
  16. {
  17. this.mediaType = type;
  18. }
  19. protected override HttpMethod getMethod()
  20. {
  21. return HttpMethod.Put;
  22. }
  23. protected override HttpContent? handleData<T>(T data)
  24. {
  25. if (mediaType == MediaType.APPLICATION_JSON_VALUE)
  26. {
  27. //请求内容在请求体中时使用
  28. return new StringContent(JsonConvert.SerializeObject(data, new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" }), Encoding.UTF8, "application/json");
  29. }
  30. else
  31. {
  32. return new FormUrlEncodedContent(convertObj2Dict(data));
  33. }
  34. }
  35. }
  36. }