StreamReaderを利用しクライアントからPOSTで送信された情報を受信する (POSTされた生データの受信とダンプ) - ASP.NET

クライアントからPOSTで送信された情報をそのまま取得したい場合があります。StreamReaderクラスを用いるとクライアントから送られたデータを直接読み取れます。
今回はStreamReaderクラスを用い、クライアントからPOSTされたデータをテキストにダンプするWebアプリケーションを作成してみます。

作成手順(ジェネリック ハンドラーの場合)

[新規作成]>[プロジェクト] から[ASP.NET 空のWebアプリケーション]を新規作成します。次に、ソリューションエクスプローラのプロジェクトノードを右クリックし、[追加]>[新しい項目]メニューを選択し、[ジェネリック ハンドラー]を追加します。

ジェネリックハンドラを追加したプロジェクトは下図の通りです。


ジェネリックハンドラーの ProcessRequestメソッドに以下のコードを記述します。
public void ProcessRequest(HttpContext context)
{
  context.Response.ContentType = "text/plain";
  context.Response.Write("Accept");

  StreamReader reader = new StreamReader(context.Request.InputStream);
  string str = reader.ReadToEnd();
  StreamWriter sw = new StreamWriter(@"c:\data\dump.txt", true, System.Text.Encoding.ASCII);
  sw.Write(str);
  sw.Close();
}
Webアプリケーションをサーバーに配置し、ジェネリックハンドラのURLにPOSTで送信すると、サーバー側で受信したデータをc:\data\dump.txt ファイルに書き込みます。

作成手順(Webフォームの場合)

プロジェクトの作成

ASP.NETアプリケーションを新規作成します。作成後Webフォームを新規追加します。

UI

以下のUIを作成します。


aspxのコードは以下になります。
PostDest.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PostDest.aspx.cs" Inherits="HtmlForm.PostDest" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>

PostForm.html

データを送信するためのHTMLフォームです。
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
  <form method="post" action="PostDest.aspx">
    <div>Value-01<input name="value01" type="text" /></div>
    <div>Value-02<input name="value02" type="text" /></div>
    <div>Value-03
      <select id="Select1" name="value03">
        <option>要素1</option>
        <option>要素2</option>
        <option>要素3</option>
        <option>要素4</option>
        <option>要素5</option>
      </select>
    </div>
    <div>Value-04<br/>
      <input id="Radio1" name="RadioGroup1" type="radio" /><label for="Radio1">ラジオボタン 要素1</label><br />
      <input id="Radio2" name="RadioGroup1" type="radio" /><label for="Radio2">ラジオボタン 要素2</label><br />
      <input id="Radio3" name="RadioGroup1" type="radio" /><label for="Radio3">ラジオボタン 要素3</label><br />
    </div>
    <div>Value-05<br />
      <input id="Chkbox1" name="checkbox1" type="checkbox" /><label for="Checkbox1">チェック項目1</label><br />
    </div>
    <div>Value-06<br />
      <input id="Hidden1" name="hiddenfield1" type="hidden" value="Test Value" /><br />
    </div>
    <input type="submit" value="POST" />
  </form>
</body>
</html>

コード

以下のコードを記述します。
PostDest.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace HtmlForm
{
  public partial class PostDest : System.Web.UI.Page
  {
    protected void Page_Load(object sender, EventArgs e)
    {
      StreamReader reader = new StreamReader(Request.InputStream);
      string str = reader.ReadToEnd();
      reader.Close();

      Label1.Text = str;
    }
  }
}

解説

Request.InputStream からStreamReaderを取得します。StreamReaderのReadToEnd()メソッドを呼び出しPOSTされたデータをすべて受け取ります。受け取ったデータをLabel1に表示します。

実行結果

送信用htmlファイル(PostForm.html)を表示します。下図の画面が表示されます。


テキストボックスや各種フィールドに値を設定します。設定後[POST]ボタンをクリックします。


PostDest.aspxに遷移しポストされたデータの中身が画面に表示されます。

著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2024-01-06
作成日: 2010-10-25
iPentec all rights reserverd.