csharp-sdk-upm/Common/Http/LCHttpUtils.cs

50 lines
2.0 KiB
C#
Raw Normal View History

using System.Linq;
using System.Text;
using System.Net.Http;
namespace LeanCloud.Common {
2020-03-10 16:54:50 +08:00
public static class LCHttpUtils {
public static void PrintRequest(HttpClient client, HttpRequestMessage request, string content = null) {
if (client == null) {
return;
}
if (request == null) {
return;
}
StringBuilder sb = new StringBuilder();
sb.AppendLine("=== HTTP Request Start ===");
sb.AppendLine($"URL: {request.RequestUri}");
sb.AppendLine($"Method: {request.Method}");
sb.AppendLine($"Headers: ");
foreach (var header in client.DefaultRequestHeaders) {
sb.AppendLine($"\t{header.Key}: {string.Join(",", header.Value.ToArray())}");
}
foreach (var header in request.Headers) {
sb.AppendLine($"\t{header.Key}: {string.Join(",", header.Value.ToArray())}");
}
if (request.Content != null) {
foreach (var header in request.Content.Headers) {
sb.AppendLine($"\t{header.Key}: {string.Join(",", header.Value.ToArray())}");
}
}
if (!string.IsNullOrEmpty(content)) {
sb.AppendLine($"Content: {content}");
}
sb.AppendLine("=== HTTP Request End ===");
2020-03-10 16:54:50 +08:00
LCLogger.Debug(sb.ToString());
}
public static void PrintResponse(HttpResponseMessage response, string content = null) {
StringBuilder sb = new StringBuilder();
sb.AppendLine("=== HTTP Response Start ===");
sb.AppendLine($"URL: {response.RequestMessage.RequestUri}");
sb.AppendLine($"Status Code: {response.StatusCode}");
if (!string.IsNullOrEmpty(content)) {
sb.AppendLine($"Content: {content}");
}
sb.AppendLine("=== HTTP Response End ===");
2020-03-10 16:54:50 +08:00
LCLogger.Debug(sb.ToString());
}
}
}