如何使用 C# POST 方法从网站向 HttpHandler post 参数

How can I post parameters from web site to HttpHandler with POST method using C#

我有一个 HttpHandler 来下载文件。此处理程序在另一台服务器中 http://localhost:5300 than my website (http://localhost:5400)。现在下载的参数是FileId.

我的网站上有 link 特定文件供下载:http://localhost:5300/App/DownloadFile.axd?FileId=123

我想添加安全令牌以供下载。我想使用 POST 方法将此令牌发送给处理程序,而不是像 FileId.

中的 url

如何使用 POST 方法创建 link 或操作以使用 http 处理程序下载文件?

HttpHandler 下载文件:

public class DownloadFile : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Clear();
            context.Response.HeaderEncoding = Encoding.UTF8;
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.AddHeader("Content-Disposition",
                "attachment; filename=" + HttpUtility.UrlEncode("FileName", Encoding.UTF8)); //return FileName from DB
            context.Response.AddHeader("Content-Length", "Length"); //return Length from DB
            context.Response.Charset = "UTF8";
            context.Response.ContentType = "ContentType"; //return ContentType from DB
            context.Response.BinaryWrite(new byte[]{}); //return binary from DB
            context.Response.Flush();
            context.Response.End();
        }

        #endregion
    }

客户端

function CallHandler() {
    $.ajax({
        url: "DownloadFile.ashx",
        contentType: "application/json; charset=utf-8",
        type: 'POST',
        dataType: "json",
        data: JSON.stringify({Field: "123", SecurityToken: "wqe76qw7e6q7we679q6e7qw6e79qwey9"}),
        success: OnComplete,
        error: OnFail
    });
}

function OnComplete(result) {
    // display something while its downloading -- animation/message
}
function OnFail(result) {
    // handle error
}

服务器端

public void ProcessRequest(HttpContext context)
{
    var jsonSerializer = new JavaScriptSerializer();
    var jsonString = String.Empty;

    context.Request.InputStream.Position = 0;
    using (var inputStream = new StreamReader(context.Request.InputStream))
    {
        jsonString = inputStream.ReadToEnd();
    }

    var workItem = jsonSerializer.Deserialize<List<WorkItem>>(jsonString);
    if(workItem != null && do your check for security token)
    {
            context.Response.ContentType = "application/download";
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.AddHeader("Content-Disposition",
                "attachment; filename=" + HttpUtility.UrlEncode("FileName", Encoding.UTF8)); //return FileName from DB
            context.Response.AddHeader("Content-Length", "Length"); //return Length from DB
            context.Response.Charset = "UTF8";
            context.Response.BinaryWrite(new byte[]{}); //return binary from DB
            context.Response.Write();
            context.Response.Flush();
    }
}

public class WorkItem
{
    public string Field { get; set; }
    public string SecurityToken { get; set; }
}

这应该有效;]