次の方法で共有


HttpException コンストラクタ (Int32, String, Int32)

HTTP 応答ステータス コード、エラー メッセージ、および例外コードを使用して、HttpException クラスの新しいインスタンスを初期化します。

名前空間: System.Web
アセンブリ: System.Web (system.web.dll 内)

構文

'宣言
Public Sub New ( _
    httpCode As Integer, _
    message As String, _
    hr As Integer _
)
'使用
Dim httpCode As Integer
Dim message As String
Dim hr As Integer

Dim instance As New HttpException(httpCode, message, hr)
public HttpException (
    int httpCode,
    string message,
    int hr
)
public:
HttpException (
    int httpCode, 
    String^ message, 
    int hr
)
public HttpException (
    int httpCode, 
    String message, 
    int hr
)
public function HttpException (
    httpCode : int, 
    message : String, 
    hr : int
)
適用できません。

パラメータ

  • httpCode
    クライアントに表示する HTTP 応答ステータス コード。
  • message
    例外がスローされたときに、クライアントに表示するエラー メッセージ。
  • hr
    エラーを定義する例外コード。

使用例

HttpException クラスの HttpException コンストラクタのコード例を次に示します。ユーザーが、ユーザー名と電子メール情報を所定のテキスト ボックスに入力します。空のテキスト ボックスがある場合、HttpException オブジェクトが作成され、スローされます。HttpException のエラー コードが GetHttpCode メソッドによって取得され、Web ページに表示されます。

セキュリティに関するメモセキュリティに関するメモ :

この例には、ユーザー入力を受け付けるテキスト ボックスがあります。これにより、セキュリティが脆弱になる可能性があります。既定では、ASP.NET Web ページによって、ユーザー入力にスクリプトまたは HTML 要素が含まれていないかどうかが検証されます。詳細については、「スクリプトによる攻略の概要」を参照してください。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <head>
    <title>HttpException Example</title>
<script language="VB" runat="server">
         Sub SubmitButton_Click(sender As Object, e As EventArgs)
            Try
               If Textbox1.Text.Length = 0 Or Textbox2.Text.Length = 0 Then
                  ' Raise an Exception if the username or emailid field is empty.
                  Throw New HttpException(901, "User name or e-mail ID not provided", 333)
               Else
                  MyLabel.Text = "Hello " & Textbox1.Text & "<br />"
                  MyLabel.Text += "The Weekly newsletter is mailed to :" & Textbox2.Text & "<br />"
               End If
            Catch ex As HttpException
               ' Display the error code returned by the GetHttpCode method.
            MyLabel.Text = "<h4><font color=""red"">The exception is " & ex.GetHttpCode() & _
               " - " & ex.Message & "</font></h4>"
            End Try
         End Sub

         Sub Page_Load(sender As Object, e As EventArgs)
            MyLabel.Text = ""
         End Sub
      </script>
   </head>

   <body>
      <form runat="server" id="Form1">
         <h3>HttpException Example</h3>
         Enter User name and E-mail
         <br /><br />
         User Name:
         <asp:TextBox ID="Textbox1" Runat="server"></asp:TextBox>
         <br />
         E-mail ID:
         <asp:TextBox ID="Textbox2" Runat="server"></asp:TextBox>
         <asp:Button ID="Button1" Text="Submit" OnClick="SubmitButton_Click" runat="server"/>
         <br />
         <asp:label id="MyLabel" runat="server"/>
      </form>
   </body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <head>
    <title>HttpException Example</title>
<script language="C#" runat="server">
         void SubmitButton_Click(Object sender, EventArgs e)
         {
            try
            {
               if(Textbox1.Text.Length==0 || Textbox2.Text.Length==0)
               {
                  // Raise an Exception if the username or the emailfield field is empty.
                  throw new HttpException(901,"User name or e-mail ID not provided.",333);
               }
               else
               {
                  MyLabel.Text="Hello "+Textbox1.Text+"<br />";
                  MyLabel.Text+="The Weekly newsletter is mailed to :"+
                           Textbox2.Text+"<br />";
               }
            }
            catch(HttpException ex)
            { 
               // Display the error code returned by the GetHttpCode method.
               MyLabel.Text="<h4><font color=\"red\">The exception is "+
                  ex.GetHttpCode() +" - "+ ex.Message + "</font></h4>";
            }
         }

         void Page_Load(object sender,EventArgs e)
         {
            MyLabel.Text="";
         }
      </script>
   </head>

   <body>
      <form runat="server" id="Form1">
         <h3>HttpException Example</h3>
         Enter UserName and Email
         <br /><br />
         UserName :
         <asp:TextBox ID="Textbox1" Runat="server"></asp:TextBox>
         <br />
         E-mail ID :
         <asp:TextBox ID="Textbox2" Runat="server"></asp:TextBox>
         <asp:Button ID="Button1" Text="Submit" OnClick="SubmitButton_Click" runat="server" />
         <br />
         <asp:label id="MyLabel" runat="server" />
      </form>
   </body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <head>
    <title>HttpException Example</title>
<script language="VJ#" runat="server">
        void SubmitButton_Click(Object sender, EventArgs e)
        {
            try {
                if(Textbox1.get_Text().get_Length()==0 || 
                    Textbox2.get_Text().get_Length()==0) {
                    // Raise an Exception if the username or the emailfield 
                    // field is empty.
                    throw new HttpException(901,"User name or e-mail " 
                        + "ID not provided.",333);
                }
                else {
                    MyLabel.set_Text("Hello "+Textbox1.get_Text()+"<br />");
                    MyLabel.set_Text(MyLabel.get_Text() 
                        + "The Weekly newsletter is mailed to :"
                        + Textbox2.get_Text()+"<br />");
                }
            }
            catch(HttpException ex) { 
                // Display the error code returned by the GetHttpCode method.
                MyLabel.set_Text("<h4><font color=\"red\">The exception is " 
                    + ex.GetHttpCode() +" - "+ ex.get_Message() 
                    + "</font></h4>");
            }
        } //SubmitButton_Click

        void Page_Load(Object sender,EventArgs e)
        {
            MyLabel.set_Text("");
        } //Page_Load
        </script>
   </head>

   <body>
      <form runat="server" id="Form1">
         <h3>HttpException Example</h3>
         Enter UserName and Email
         <br /><br />
         UserName :
         <asp:TextBox ID="Textbox1" Runat="server"></asp:TextBox>
         <br />
         E-mail ID :
         <asp:TextBox ID="Textbox2" Runat="server"></asp:TextBox>
         <asp:Button ID="Button1" Text="Submit" OnClick="SubmitButton_Click" runat="server" />
         <br />
         <asp:label id="MyLabel" runat="server" />
      </form>
   </body>
</html>

プラットフォーム

Windows 98,Windows Server 2000 SP4,Windows CE,Windows Millennium Edition,Windows Mobile for Pocket PC,Windows Mobile for Smartphone,Windows Server 2003,Windows XP Media Center Edition,Windows XP Professional x64 Edition,Windows XP SP2,Windows XP Starter Edition

Microsoft .NET Framework 3.0 は Windows Vista,Microsoft Windows XP SP2,および Windows Server 2003 SP1 でサポートされています。

バージョン情報

.NET Framework

サポート対象 : 3.0,2.0,1.1,1.0

参照

関連項目

HttpException クラス
HttpException メンバ
System.Web 名前空間
InnerException

その他の技術情報

スクリプトによる攻略の概要