Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions APSIM.POStats.Shared/WebUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Http.Headers;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace APSIM.POStats.Shared
Expand Down Expand Up @@ -49,7 +50,8 @@ private static async Task<string> RequestAsync(RequestType type, string requestU
response = await httpClient.PostAsync(requestUrl, new StringContent(jsonString, Encoding.UTF8, "application/json"));
}

string body = await response.Content.ReadAsStringAsync();
string fullResponse = await response.Content.ReadAsStringAsync();
string body = GetHtmlBodyContent(fullResponse);

Console.WriteLine($"Status: {(int)response.StatusCode} {response.StatusCode}");
if (response.StatusCode >= HttpStatusCode.BadRequest)
Expand All @@ -61,8 +63,8 @@ private static async Task<string> RequestAsync(RequestType type, string requestU
output += $"Request:\n{response}\n";
output += $"Response:\n{body}\n";

Console.WriteLine($"Error sending POST Request\n{output}");
throw new Exception($"Error sending POST Request\n{output}");
Console.WriteLine($"Error sending {type} for URL {requestUrl} Request\n{output}");
throw new Exception($"Error sending {type} for URL {requestUrl} Request\n{output}");
Comment on lines 63 to +67
}
return body;
}
Expand All @@ -82,5 +84,30 @@ public static async Task<string> PostAsync<T>(string requestUrl, T jsonObject, s
{
return await RequestAsync(RequestType.POST, requestUrl, JsonSerializer.Serialize(jsonObject), authorizationToken);
}

/// <summary>
/// Extract the content of the body tag from an HTML response. If no body tag is found, return the original content.
/// </summary>
/// <param name="content">The HTML content to extract the body from.</param>
/// <returns>The content of the body tag, or the original content if no body tag is found.</returns>
private static string GetHtmlBodyContent(string content)
{
if (string.IsNullOrEmpty(content))
return content;

// Avoid regex work for non-HTML responses (e.g. JSON).
if (content.IndexOf("<body", StringComparison.OrdinalIgnoreCase) < 0)
return content;

Match bodyMatch = Regex.Match(content, @"<body\b[^>]*>([\s\S]*?)</body>", RegexOptions.IgnoreCase);
string bodyHtml = bodyMatch.Success ? bodyMatch.Groups[1].Value : content;

// Convert to plain text for cleaner error output.
bodyHtml = Regex.Replace(bodyHtml, @"<script\b[^>]*>[\s\S]*?</script>", string.Empty, RegexOptions.IgnoreCase);
bodyHtml = Regex.Replace(bodyHtml, @"<style\b[^>]*>[\s\S]*?</style>", string.Empty, RegexOptions.IgnoreCase);
string text = Regex.Replace(bodyHtml, @"<[^>]+>", " ");
text = WebUtility.HtmlDecode(text);
return Regex.Replace(text, @"\s+", " ").Trim();
}
Comment thread
ric394 marked this conversation as resolved.
}
}
Loading