{"record":{"id":"dd7212dc098b3a4b","repo":"NickeManarin/ScreenToGif","slug":"empty-response-from-server-when-getting-language-c","errorCode":null,"errorMessage":"Empty response from server when getting language codes","messagePattern":"Empty response from server when getting language codes","errorType":"exception","errorClass":"WebException","httpStatus":null,"severity":"error","filePath":"Other/Translator/TranslatorWindow.xaml.cs","lineNumber":709,"sourceCode":"                \"yo-BJ;zgh;zh;zh-Hans-HK;zh-Hans-MO;zh-Hant;zu\").Split(';').ToList();\n    }\n\n    private async Task<IEnumerable<string>> GetLanguageCodesAsync()\n    {\n        var path = await GetLanguageCodesPathAsync();\n\n        if (string.IsNullOrEmpty(path))\n            throw new WebException(\"Can't get language codes. Path to language codes is null\");\n\n        var request = (HttpWebRequest)WebRequest.Create(path);\n        request.UserAgent = \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.79 Safari/537.36 Edge/14.14393\";\n\n        var response = (HttpWebResponse)await request.GetResponseAsync();\n\n        using (var resultStream = response.GetResponseStream())\n        {\n            if (resultStream == null)\n                throw new WebException(\"Empty response from server when getting language codes\");\n\n            using (var reader = new StreamReader(resultStream))\n            {\n                var result = await reader.ReadToEndAsync();\n\n                var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(result),\n                    new System.Xml.XmlDictionaryReaderQuotas());\n\n                var json = await Task<XElement>.Factory.StartNew(() => XElement.Load(jsonReader));\n                var languages = json.Elements();\n\n                return await Task.Factory.StartNew(() => languages.Where(x => x.XPathSelectElement(\"defs\").Value != \"0\").Select(x => x.XPathSelectElement(\"lang\").Value));\n            }\n        }\n    }\n\n    private async Task<string> GetLanguageCodesPathAsync()\n    {","sourceCodeStart":691,"sourceCodeEnd":727,"githubUrl":"https://github.com/NickeManarin/ScreenToGif/blob/a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd/Other/Translator/TranslatorWindow.xaml.cs#L691-L727","documentation":"Thrown by GetLanguageCodesAsync after it successfully obtains a download path but response.GetResponseStream() returns null. This means the HTTP response object was created but no response body stream was available — a rare condition indicating a server-side or transport-level problem.","triggerScenarios":"HttpWebResponse.GetResponseStream() returns null on the second request (the actual language-codes JSON download). This can happen with certain proxy servers, keep-alive connection drops, or when the server sends headers but immediately closes the connection without a body.","commonSituations":"Corporate proxy or firewall that strips or closes connections mid-response. Transient server-side error where datahub.io returns a 200 but no body. Unstable network connection that drops after the initial handshake.","solutions":["Retry the request with a fresh HttpWebRequest after a short delay.","Set request.KeepAlive = false to force a new connection rather than reusing a dropped one.","Fall back to the hardcoded language code list already present in the class.","Check proxy configuration and disable keep-alive if behind a problematic proxy."],"exampleFix":"// before\nusing (var resultStream = response.GetResponseStream())\n{\n    if (resultStream == null)\n        throw new WebException(\"Empty response from server when getting language codes\");\n\n// after: retry once before throwing\nusing (var resultStream = response.GetResponseStream())\n{\n    if (resultStream == null)\n    {\n        response.Dispose();\n        request.KeepAlive = false;\n        response = (HttpWebResponse)await request.GetResponseAsync();\n        resultStream = response.GetResponseStream();\n    }\n    if (resultStream == null)\n        throw new WebException(\"Empty response from server when getting language codes\");","handlingStrategy":"retry","validationCode":"request.KeepAlive = false;\nrequest.Timeout = 15000;\n// Validate before consuming\nvar response = (HttpWebResponse)await request.GetResponseAsync();\nif (response.ContentLength == 0)\n    return null;","typeGuard":null,"tryCatchPattern":"for (int attempt = 0; attempt < 2; attempt++)\n{\n    var stream = response.GetResponseStream();\n    if (stream != null) return stream;\n    response.Dispose();\n    response = (HttpWebResponse)await request.GetResponseAsync();\n}\nthrow new WebException(\"Empty response from server when getting language codes\");","preventionTips":["Set request.KeepAlive = false to avoid stale connection reuse.","Check response.ContentLength > 0 before reading the stream.","Implement a single retry with exponential backoff for transient null-stream conditions."],"tags":["network","http","localization","translator","proxy"],"backgroundTag":null,"analyzedSha":"a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd","analyzedAt":"2026-08-13T11:12:06.147Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}