You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

提交大负载时触发System.UriFormatException:URI字符串过长

问题解决:提交长文本时的UriFormatException错误

错误原因

你手动调用Uri.EscapeDataString处理超长文本时,生成的编码字符串长度超出了URI规范允许的上限,从而触发UriFormatException。实际上,使用MultipartFormDataContent提交表单时,不需要手动对内容做URL编码,HttpClient会自动处理内容的编码与传输。

修正后的代码

if (Input.Text.Replace(" ", "").Length == 0) {
    PGD.Content = "You must fill out the text box!";
    return;
}
try
{
    using (var wc = new HttpClient())
    {
        wc.DefaultRequestHeaders.Add("User-Agent", "FlameIgniterDPasteTool");
        var x1 = new MultipartFormDataContent
        {
            // 移除Uri.EscapeDataString,直接传入原始文本
            { new StringContent(Input.Text, Encoding.UTF8), "content" }
        };
        HttpResponseMessage res = await wc.PostAsync("https://dpaste.com/api/", x1);
        // 等待异步读取完成,避免直接赋值Task对象
        PGD.Content = await res.Content.ReadAsStringAsync();
    }
}
catch (Exception ex) {
    PGD.Content = "Error: " + ex.ToString();
}
Progress.IsEnabled = false;
await Task.Delay(1000);
Progress.IsEnabled = true;

额外说明

  • 原代码中PGD.Content = res.Content.ReadAsStringAsync();会直接将Task<string>对象赋值给Content,添加await才能获取到实际的响应内容。
  • 移除手动编码后,Multipart表单会以网站支持的方式传输长文本,适配目标站点的上传能力。

内容的提问来源于stack exchange,提问作者SpeedSonic0

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 03:34:56