{"record":{"id":"02b32724a791b974","repo":"NickeManarin/ScreenToGif","slug":"empty-response-from-server-when-getting-language-c-02b327","errorCode":null,"errorMessage":"Empty response from server when getting language codes path","messagePattern":"Empty response from server when getting language codes path","errorType":"exception","errorClass":"WebException","httpStatus":null,"severity":"warning","filePath":"ScreenToGif/Windows/Other/Localization.xaml.cs","lineNumber":444,"sourceCode":"                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    {\n        var request = (HttpWebRequest)WebRequest.Create(\"https://datahub.io/core/language-codes/datapackage.json\");\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        request.Proxy = WebHelper.GetProxy();\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 path\");\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\n                return await Task.Factory.StartNew(() => json.XPathSelectElement(\"resources\")?.Elements().First(x => x.XPathSelectElement(\"name\")?.Value == \"ietf-language-tags_json\").XPathSelectElement(\"path\")?.Value);\n            }\n        }\n    }\n\n    private void UpdateIndexes()\n    {\n        var actualIndex = 0;","sourceCodeStart":426,"sourceCodeEnd":462,"githubUrl":"https://github.com/NickeManarin/ScreenToGif/blob/a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd/ScreenToGif/Windows/Other/Localization.xaml.cs#L426-L462","documentation":"A WebException thrown inside GetLanguageCodesPathAsync — the FIRST network call in the language-code pipeline — after WebRequest.Create(\"https://datahub.io/core/language-codes/datapackage.json\") succeeds with an HttpWebResponse but response.GetResponseStream() returns null. This guard protects the JSON parse that extracts the ietf-language-tags_json path. It is the upstream sibling of error 45: if this path request yields a null stream, GetLanguageCodesPathAsync cannot even resolve the URL, so error 44's null-path guard would later fire unless this throws first.","triggerScenarios":"GetResponseAsync() to datahub.io returned a non-null HttpWebResponse but GetResponseStream() is null. Same transport/proxy edge as error 45 but for the datapackage.json request specifically: broken proxy body-stripping, empty 200 from CDN, TLS early close after headers.","commonSituations":"No/offline network where a captive portal returns an empty 200; a transparent proxy stripping the JSON body; datahub.io CDN returned Content-Length 0; WebHelper.GetProxy() points at a proxy that drops response bodies; DNS hijack returning an empty page; the datahub.io endpoint is temporarily degraded.","solutions":["Confirm internet reachability and that https://datahub.io/core/language-codes/datapackage.json returns valid JSON (curl/browser).","Check/adjust WebHelper.GetProxy() — disable the custom proxy or use the system default if it is stripping bodies.","Retry the request once with a short delay; null-stream responses are usually transient.","Fall back to a hardcoded language-codes URL when GetLanguageCodesPathAsync fails, bypassing the datapackage indirection.","Inspect response.StatusCode and ContentLength before treating null stream as fatal — surface the actual status to the user."],"exampleFix":"// before\nvar response = (HttpWebResponse)await request.GetResponseAsync();\nusing (var resultStream = response.GetResponseStream())\n{\n    if (resultStream == null)\n        throw new WebException(\"Empty response from server when getting language codes path\");\n\n// after: status check + retry + fallback URL\nif ((int)response.StatusCode >= 400)\n    throw new WebException($\"datahub.io returned {response.StatusCode}\");\n\nusing (var resultStream = response.GetResponseStream())\n{\n    if (resultStream == null)\n    {\n        LogWriter.Log(\"Empty datapackage.json stream; using fallback language-codes URL.\");\n        return \"https://raw.githubusercontent.com/datasets/language-codes/master/data/ietf-language-tags.json\";\n    }","handlingStrategy":"fallback","validationCode":"// Verify reachability of the datapackage endpoint before the request.\nvar endpoint = \"https://datahub.io/core/language-codes/datapackage.json\";\nif (!await NetworkHelper.IsReachableAsync(endpoint))\n    return \"https://raw.githubusercontent.com/datasets/language-codes/master/data/ietf-language-tags.json\";\n\nvar request = (HttpWebRequest)WebRequest.Create(endpoint);","typeGuard":"static bool HasReadableBody(HttpWebResponse response) =>\n    response != null && (int)response.StatusCode < 400 && response.ContentLength != 0;\n\nvar response = (HttpWebResponse)await request.GetResponseAsync();\nif (!HasReadableBody(response))\n    return /* fallback language-codes URL */;","tryCatchPattern":"string path = null;\ntry\n{\n    path = await GetLanguageCodesPathAsync();\n}\ncatch (WebException wex) when (wex.Message.Contains(\"language codes path\"))\n{\n    LogWriter.Log(wex, \"datapackage.json body unreadable; using fallback language-codes URL.\");\n    path = \"https://raw.githubusercontent.com/datasets/language-codes/master/data/ietf-language-tags.json\";\n}\nreturn path;","preventionTips":["Pin a direct, versioned URL for language codes rather than resolving through datapackage.json each run.","Cache the last-good path on disk; reuse it when the upstream is unreachable/degraded.","Monitor the datahub.io dataset for schema/resource-name changes with an automated check.","Treat the datapackage indirection as optional, not load-bearing — the feature must work with a static fallback URL."],"tags":["network","webexception","http","stream","localization","datahub","proxy","upstream-dependency"],"backgroundTag":null,"analyzedSha":"a4d0a67c2131cd048ceec86cd40afc2f1a06f2fd","analyzedAt":"2026-08-13T11:12:06.147Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}