通过HttpClient POST

时间:2017-07-14 19:47:58

标签: c# uwp richeditbox

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

TL:DR :使用TextBox使用HttpClientPOST发送字符串有效,但在使用TextBox替换RichEditBox时却无效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; }

以下是完整的错误消息:

  

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

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

修改

代码示例:

as.character

0 个答案:

没有答案