I'm using HttpClient to POST a user created string to a server API and get the result. Previously I used a TextBox to allow the user to enter the string but I wanted pretty colours so I tried to replace the TextBox with a RichEditBox but failed miserably. When using the TextBox everything worked fine but when using the RichEditBox I get an "Access denied" message. I get the text from the RichEditBox with the Document.GetText method. I can get it to work by either inserting a static string in the place where I get the text or in the place where I send the string into a FormUrlEncodedContent. The string is edited before and after adding the text from the RichEditBox, and sent to another method.
TL:DR: Sending a string from a TextBox with a HttpClient using POST works but not when replacing the TextBox with a RichEditBox.
Here's the full error message:
System.UnauthorizedAccessException: 'Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))'
Does anyone have a solution or an explanation to why this is happening?
Edit
Code sample:
private async void RichEditBox_KeyUp(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
{
await RunCode(); //The error points to this line
}
}
private async void RunCode()
{
string code = CodeBefore;
foreach (RichEditBox tb in editStack.Children) //If I comment out the foreach loop I don't get any errors
{
if (tb.Tag.ToString() == "input")
{
tb.Document.GetText(Windows.UI.Text.TextGetOptions.None, out string thisLine);
code += thisLine;
}
}
code += CodeAfter;
await RunCSharp(code);
}
private async Task<Code> RunCSharp(string code)
{
Code re = new Code();
using (HttpClient client = new HttpClient())
{
FormUrlEncodedContent content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("LanguageChoiceWrapper", "1"),
new KeyValuePair<string, string>("Program", code), //If I replace code with a string I don't get any errors
new KeyValuePair<string, string>("ShowWarnings", "false")
});
try
{
HttpResponseMessage msg = await client.PostAsync("http://mywebaddress.com/run", content);
re = JsonConvert.DeserializeObject<Code>(await msg.Content.ReadAsStringAsync());
}
catch { }
}
return re;
}