ColdFusion ページから POST される HTTPHandler を既に作成しており、正常に動作します。今、ASP.NET で Web アプリケーションを作成しようとしているので、.aspx ページから .ashx ハンドラーにフォームを投稿できます。
アプリケーション トレース (trace.axd) には、最後の 3 つのエントリとして次のように表示されます。
2 8/14/2009 1:53:56 PM /Default.aspx 200 GET View Details
3 8/14/2009 1:54:04 PM /Default.aspx 200 POST View Details
4 8/14/2009 1:54:13 PM /UploadHandler.ashx 401 POST View Details
.ashx ファイルにブレークポイントがありますが、到達することはありません (ステータス コード 401 が原因だと思います)。ハンドラーに POST しようとしているdefault.aspxのコード スニペットを次に示します。
protected void UploadHandlerButton_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
try
{
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(BuildFormData());
string baseAddress = "http://" + Environment.MachineName;
string pathInfo = Page.ResolveUrl("UploadHandler.ashx");
string URI = baseAddress + pathInfo;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(URI);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
}
catch (Exception someError)
{
LogText("FAILURE: " + someError.Message);
}
}
}
UploadHandler.ashxファイルからのコード スニペットを次に示します (ただし、これには達していないようです)。
public void ProcessRequest(HttpContext context)
{
string returnURL = context.Request.ServerVariables["HTTP_REFERER"];
string message;
message = UploadFile(context);
StringBuilder msgReturn = new StringBuilder(returnURL);
msgReturn.Append("?n=");
msgReturn.Append(HttpUtility.UrlEncode(TRIMrecNumAssigned));
msgReturn.Append("&m=");
msgReturn.Append(HttpUtility.UrlEncode(message));
context.Response.Redirect(msgReturn.ToString());
}
default.aspx と UploadHandler.ashx は両方とも、私のローカルホストの仮想ディレクトリのルートにあります。ディレクトリ セキュリティは現在、"匿名アクセス" チェック済みおよび "統合 Windows 認証" チェック済みに設定されています。
trace.axd ディスプレイの [View Details] リンクをクリックすると、フォーム コレクション内のすべてのデータが表示され、処理されることを期待していますが、この 401 によってすべてが停止しているようです。役に立ったら、BuildFormData() という小さな関数のコードを投稿できます。
編集:次のようにハンドラーを修正しました(効果はありませんでした;同じエラーが発生します):
public void ProcessRequest(HttpContext context)
{
//-----------------------------------------------------------------------------------------
// the remainder of this block is alternative to the .Redirect and is useful for debugging.
context.Response.ContentType = "text/html";
//context.Response.Write(TRIMrecNumAssigned);
//context.Response.Write("<p>");
//context.Response.Write(msgReturn);
context.Response.Write("<H1>Trim - Kerberos Prototype for ColdFusion consuming pages</h1>");
HttpContext.Current.Trace.IsEnabled = true;
HttpContext.Current.Trace.Write(null);
HttpContext.Current.Trace.Write("-------");
HttpContext.Current.Trace.Write(context.Request.Form["txtTrimRecordType"]);
HttpContext.Current.Trace.Write(GetUserInfo());
HttpContext.Current.Trace.Write("-------");
HttpContext.Current.Trace.Write(null);
using (Html32TextWriter htw = new Html32TextWriter(context.Response.Output))
{
typeof(TraceContext)
.GetMethod("Render", BindingFlags.NonPublic | BindingFlags.Instance)
.Invoke(HttpContext.Current.Trace, new object[] { htw });
}
}