使用DeepL .NET API翻译大文件的超时及报错问题求助
解决DeepL .NET API大文件翻译报错问题
1. 调整IIS请求大小限制
.NET Framework Web API部署在IIS上时,默认请求体大小限制约为28.6MB(maxAllowedContentLength默认30000000字节),30MB文件会触发413错误。修改Web.config:
<!-- 配置IIS请求过滤的最大内容长度(示例为100MB,单位字节) --> <system.webServer> <security> <requestFiltering> <requestLimits maxAllowedContentLength="104857600" /> </requestFiltering> </security> </system.webServer> <!-- 配置ASP.NET的最大请求长度与执行超时(示例为100MB,单位KB) --> <system.web> <httpRuntime maxRequestLength="102400" executionTimeout="600" /> </system.web>
2. 升级DeepL API超时与重试策略
针对30MB+文件的翻译耗时,进一步延长超时时间,并优化重试逻辑:
var deeplClient = new DeepLClient("你的认证密钥") { // 设置整体连接超时为15分钟,覆盖大文件翻译的全流程 OverallConnectionTimeout = TimeSpan.FromMinutes(15), // 单次重试的连接超时设为5分钟 PerRetryConnectionTimeout = TimeSpan.FromMinutes(5) }; // 针对413、5xx等可重试错误配置重试策略 deeplClient.SetRetryPolicy(Policy .Handle<HttpRequestException>() .OrResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.RequestEntityTooLarge || (int)r.StatusCode >= 500) .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))));
3. 确认DeepL Pro账号权限
登录DeepL后台检查:
- 账号是否支持1GB以内的文件翻译(Pro账号默认支持)
- 调用的API端点是否为Pro版(
https://api.deepl.com/v2/document,请勿使用免费版端点)
4. 改用异步翻译模式
对于超大文件,异步模式可避免长时间占用HTTP连接:
// 异步上传文件并获取任务ID var documentHandle = await deeplClient.TranslateDocumentAsync( inputStream, sourceLang: "EN", targetLang: "ZH", waitForCompletion: false); // 轮询查询翻译状态 DocumentStatus status; do { status = await deeplClient.GetDocumentStatusAsync(documentHandle.DocumentId); await Task.Delay(TimeSpan.FromSeconds(5)); } while (status.Status is DocumentStatus.Queued or DocumentStatus.Translating); // 下载完成的翻译文件 if (status.Status == DocumentStatus.Done) { await deeplClient.DownloadDocumentAsync(documentHandle.DocumentId, outputStream); }
内容的提问来源于stack exchange,提问作者Kanchan Kumbhar
相关产品推荐
相关产品推荐

