0

我正在使用HttpClient将用户创建的字符串发布到服务器 API 并获取结果。以前我使用 aTextBox来允许用户输入字符串,但我想要漂亮的颜色,所以我尝试TextBox用 a替换,RichEditBox但失败了。使用TextBox一切正常但使用时RichEditBox我收到“拒绝访问”消息。我从RichEditBoxwithDocument.GetText方法中获取文本。我可以通过在获取文本的位置或将字符串发送到FormUrlEncodedContent. 在从 中添加文本之前和之后对字符串进行编辑,然后将其RichEditBox发送到另一个方法。

TL:DR:从 a 发送一个TextBox带有 a的字符串可以HttpClient使用,POST但在TextBox用 a替换时不行RichEditBox

这是完整的错误消息:

System.UnauthorizedAccessException: '访问被拒绝。(来自 HRESULT 的异常:0x80070005 (E_ACCESSDENIED))'

有没有人有解决方案或解释为什么会发生这种情况?

编辑

代码示例:

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;
}
4

0 回答 0