NickeManarin/ScreenToGif · error · WebException

Empty response from server when getting language codes

Error message

Empty response from server when getting language codes

What it means

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.

Source

Thrown at Other/Translator/TranslatorWindow.xaml.cs:709

                "yo-BJ;zgh;zh;zh-Hans-HK;zh-Hans-MO;zh-Hant;zu").Split(';').ToList();
    }

    private async Task<IEnumerable<string>> GetLanguageCodesAsync()
    {
        var path = await GetLanguageCodesPathAsync();

        if (string.IsNullOrEmpty(path))
            throw new WebException("Can't get language codes. Path to language codes is null");

        var request = (HttpWebRequest)WebRequest.Create(path);
        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";

        var response = (HttpWebResponse)await request.GetResponseAsync();

        using (var resultStream = response.GetResponseStream())
        {
            if (resultStream == null)
                throw new WebException("Empty response from server when getting language codes");

            using (var reader = new StreamReader(resultStream))
            {
                var result = await reader.ReadToEndAsync();

                var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(result),
                    new System.Xml.XmlDictionaryReaderQuotas());

                var json = await Task<XElement>.Factory.StartNew(() => XElement.Load(jsonReader));
                var languages = json.Elements();

                return await Task.Factory.StartNew(() => languages.Where(x => x.XPathSelectElement("defs").Value != "0").Select(x => x.XPathSelectElement("lang").Value));
            }
        }
    }

    private async Task<string> GetLanguageCodesPathAsync()
    {

View on GitHub (pinned to a4d0a67c21)

Solutions

  1. Retry the request with a fresh HttpWebRequest after a short delay.
  2. Set request.KeepAlive = false to force a new connection rather than reusing a dropped one.
  3. Fall back to the hardcoded language code list already present in the class.
  4. Check proxy configuration and disable keep-alive if behind a problematic proxy.

Example fix

// before
using (var resultStream = response.GetResponseStream())
{
    if (resultStream == null)
        throw new WebException("Empty response from server when getting language codes");

// after: retry once before throwing
using (var resultStream = response.GetResponseStream())
{
    if (resultStream == null)
    {
        response.Dispose();
        request.KeepAlive = false;
        response = (HttpWebResponse)await request.GetResponseAsync();
        resultStream = response.GetResponseStream();
    }
    if (resultStream == null)
        throw new WebException("Empty response from server when getting language codes");
Defensive patterns

Strategy: retry

Validate before calling

request.KeepAlive = false;
request.Timeout = 15000;
// Validate before consuming
var response = (HttpWebResponse)await request.GetResponseAsync();
if (response.ContentLength == 0)
    return null;

Try / catch

for (int attempt = 0; attempt < 2; attempt++)
{
    var stream = response.GetResponseStream();
    if (stream != null) return stream;
    response.Dispose();
    response = (HttpWebResponse)await request.GetResponseAsync();
}
throw new WebException("Empty response from server when getting language codes");

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of NickeManarin/ScreenToGif@a4d0a67c21 (2026-08-13). Data as JSON: /api/errors/dd7212dc098b3a4b. Report an issue: GitHub.