System.Net.WebException:基礎となる接続が閉じられました。受信時に予期しないエラーが発生しました

StackOverflow https://stackoverflow.com/questions/751056

  •  09-09-2019
  •  | 
  •  

質問

URLからWebページのHTMLコンテンツの文字列を返すメソッドをC#で作成しようとしています。いくつかの異なる方法を試しましたが、エラーが発生します System.Net.WebException:基礎となる接続が閉じられました。受信時に予期しないエラーが発生しました。

以下はローカルでは正常に動作しますが、リモート サーバーで実行すると上記のエラーが発生します。

  public static string WebPageRead(string url)
    {
        string result = String.Empty;

        WebResponse response = null;
        StreamReader reader = null;

        try
        {
            if (!String.IsNullOrEmpty(url))
            {
                HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
                request.Method = "GET";
                request.KeepAlive = false;
                request.ProtocolVersion = HttpVersion.Version10;

                response = request.GetResponse();
                reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                result = reader.ReadToEnd();
            }
        }
        catch (Exception exc)
        {
            throw exc;
        }
        finally
        {
            if (reader != null)
            {
                reader.Close();
            }

            if (response != null)
            {
                response.Close();
            }
        }

        return result;
    }
役に立ちましたか?

解決 2

ご回答ありがとうございます。問題はリモート サーバー上の DNS の問題が原因でした。確認のために、最終的に次のコードを使用しました。

    public static string WebPageRead(string url)
    {
        string content = String.Empty;

        if (!String.IsNullOrEmpty(url))
        {
            HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;

            if (request != null)
            {
                request.Method = "GET";
                request.KeepAlive = false;
                request.ProtocolVersion = HttpVersion.Version10;

                try
                {
                    using (WebResponse response = request.GetResponse())
                    {
                        using (Stream stream = response.GetResponseStream())
                        {
                            using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
                            {
                                content = reader.ReadToEnd();
                            }
                        }
                    }
                }
                catch (Exception exc)
                {
                    throw exc;
                }
            }
        }                    

        return content;
    }

他のヒント

おそらくこれは問題ではありませんが、次のことを試してください。

public static string WebPageRead(string url)
{
    if (String.IsNullOrEmpty(url))
    {
        return null;
    }

    HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
    if (request == null)
    {
        return null;
    }

    request.Method = "GET";
    request.KeepAlive = false;
    request.ProtocolVersion = HttpVersion.Version10;

    using (WebResponse response = request.GetResponse())
    {
        using (Stream stream = response.GetResponseStream())
        {
            using (StreamReader reader = 
                       new StreamReader(stream, Encoding.UTF8))
            {
                return reader.ReadToEnd();
            }
        }
    }
}

既知の適切な URL でこれを試すことを提案する以前の回答を繰り返します。バージョンを 1.0 に設定する行をコメントアウトして、既知の正常な HTTP 1.1 URL でこれを試す必要があることを付け加えます。それがうまくいけば、かなり絞り込まれます。

以前にこのような問題が発生しましたが、問題が発生したマシンで IE で URL を開くことで解決されました。次に、IE は、その URL を安全なサイトのリストに追加するかどうかを尋ねます。それを追加すると、その URL で機能します。

これは考えられる原因の 1 つにすぎません。他の多くの問題がこれを引き起こす可能性があります。上記の問題に加えて、私がこれを解決する最善の方法は、例外をキャッチしてリクエストを再試行することです。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top