3

HttpListenerを使用している場合、WebException 304エラーをブラウザに模倣するにはどうすればよいですか?

つまり、HttpListenerへのリクエストを受信し、HttpListenerContextを取得しました。この時点から、HTTP「304NotModified」応答がHttpListenerContext.responseを介してブラウザに効果的に返送されるように模倣/調整するにはどうすればよいですか。

編集:

次のことを試しましたが、WebException.StatusをHttpWebResponse.StatusCodeにコピーしようとするとエラーが発生します(ステータスコードは正確に3桁である必要があります)。これを修正する方法について何かアイデアはありますか?

    catch (WebException ex)
    {
        listenerContext.Response.StatusCode = (int)ex.Status;   //ERROR: The status code must be exactly three digits
        listenerContext.Response.StatusDescription = ex.Message;
        listenerContext.Response.Close();

ありがとう

4

1 に答える 1

2

私はそれを持っていると思います:

    catch (WebException ex)
    {


        if (ex.Status == WebExceptionStatus.ProtocolError)
        {
            int statusCode = (int) ((HttpWebResponse) ex.Response).StatusCode;
            listenerContext.Response.StatusCode = statusCode;
            listenerContext.Response.StatusDescription = ex.Message;
            log("WARNING", uri, "WebException/ProtocolError: " + ex.GetType() + " - " + ex.Message);
        }
        else
        {
            log("ERROR", uri, "WebException - " + ex.GetType() + " - " + ex.Message);

        }

        listenerContext.Response.Close();
    }
于 2010-04-11T04:29:11.567 に答える