Fixed the "System.UriFormatException: Invalid URI: The Uri string is too long." exception when submitting a longer hardware report.

This commit is contained in:
Michael Möller 2020-01-29 21:51:03 +01:00
parent 49367c2e45
commit 357a7a114e
4 changed files with 47 additions and 11 deletions

View File

@ -4,11 +4,11 @@
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
Copyright (C) 2009-2020 Michael Möller <mmoeller@openhardwaremonitor.org>
*/
using OpenHardwareMonitor.Utilities;
using System;
using System.IO;
using System.Net;
@ -59,10 +59,10 @@ namespace OpenHardwareMonitor.GUI {
string report =
"type=crash&" +
"version=" + Uri.EscapeDataString(version.ToString()) + "&" +
"report=" + Uri.EscapeDataString(reportTextBox.Text) + "&" +
"comment=" + Uri.EscapeDataString(commentTextBox.Text) + "&" +
"email=" + Uri.EscapeDataString(emailTextBox.Text);
"version=" + HttpUtility.UrlEncode(version.ToString()) + "&" +
"report=" + HttpUtility.UrlEncode(reportTextBox.Text) + "&" +
"comment=" + HttpUtility.UrlEncode(commentTextBox.Text) + "&" +
"email=" + HttpUtility.UrlEncode(emailTextBox.Text);
byte[] byteArray = Encoding.UTF8.GetBytes(report);
request.ContentLength = byteArray.Length;

View File

@ -4,10 +4,11 @@
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
Copyright (C) 2009-2020 Michael Möller <mmoeller@openhardwaremonitor.org>
*/
using OpenHardwareMonitor.Utilities;
using System;
using System.Drawing;
using System.IO;
@ -47,10 +48,10 @@ namespace OpenHardwareMonitor.GUI {
string report =
"type=hardware&" +
"version=" + Uri.EscapeDataString(version.ToString()) + "&" +
"report=" + Uri.EscapeDataString(reportTextBox.Text) + "&" +
"comment=" + Uri.EscapeDataString(commentTextBox.Text) + "&" +
"email=" + Uri.EscapeDataString(emailTextBox.Text);
"version=" + HttpUtility.UrlEncode(version.ToString()) + "&" +
"report=" + HttpUtility.UrlEncode(reportTextBox.Text) + "&" +
"comment=" + HttpUtility.UrlEncode(commentTextBox.Text) + "&" +
"email=" + HttpUtility.UrlEncode(emailTextBox.Text);
byte[] byteArray = Encoding.UTF8.GetBytes(report);
request.ContentLength = byteArray.Length;

View File

@ -125,6 +125,7 @@
<Compile Include="GUI\UserRadioGroup.cs" />
<Compile Include="Properties\AssemblyVersion.cs" />
<Compile Include="Utilities\HttpServer.cs" />
<Compile Include="Utilities\HttpUtility.cs" />
<Compile Include="Utilities\Logger.cs" />
<Compile Include="Utilities\PersistentSettings.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />

34
Utilities/HttpUtility.cs Normal file
View File

@ -0,0 +1,34 @@
/*
This Source Code Form is subject to the terms of the Mozilla Public
License, v. 2.0. If a copy of the MPL was not distributed with this
file, You can obtain one at http://mozilla.org/MPL/2.0/.
Copyright (C) 2020 Michael Möller <mmoeller@openhardwaremonitor.org>
*/
using System;
using System.Text;
namespace OpenHardwareMonitor.Utilities {
public class HttpUtility {
public static string UrlEncode(string s) {
int maxLength = 32765;
var sb = new StringBuilder();
int imax = s.Length / maxLength;
for (int i = 0; i <= imax; i++) {
sb.Append(
Uri.EscapeDataString(i < imax
? s.Substring(maxLength * i, maxLength)
: s.Substring(maxLength * i)));
}
return sb.ToString();
}
}
}