如何使用c# post方法将参数从网站发送到HttpHandler

本文关键字:网站 HttpHandler 参数 何使用 post 方法 | 更新日期: 2023-09-27 18:11:59

我有一个HttpHandler下载文件。这个处理程序在另一个服务器http://localhost:5300比我的网站(http://localhost:5400)。现在下载的参数是FileId。

我在我的网站上有一个链接到特定的文档下载:http://localhost:5300/App/DownloadFile.axd?FileId=123

我想添加一个安全令牌来下载。我想用POST方法发送这个令牌到处理程序,而不是在url中,如FileId。

我如何创建一个链接或动作下载一个文件与http处理程序使用POST方法?

HttpHandler DownloadFile:

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
    }

如何使用c# post方法将参数从网站发送到HttpHandler

客户端

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; }
}