如何将webmethod POST发送到HTTPHandler.ashx文件

时间:2011-09-07 17:01:47

标签: web-services coldfusion httphandler httpcontext

摘要

如何在Web服务中创建HTTPContext?或从Web服务POST到Handler.ashx?

背景

我有一个使用表单身份验证的Cold Fusion Web应用程序,但以某种方式使用此脚本实现Windows身份验证:

<cfscript>
    ws = CreateObject("webservice", "#qTrim.webServiceName#");
    ws.setUsername("#qTrim.trimAcct#");
    ws.setPassword("#qTrim.trimpwd#");
    wsString=ws.UploadFileCF("#qTrim.webserviceurl#","#objBinaryData#", "#qFiles.Filename#", "Document", "#MetaData#");
</cfscript>
  • 显然,setUsername / setPassword值映射到单个Windows域帐户,这适用于生产。 (Web服务是用C#编写的,用.Net 4.0构建。它必须由这个域帐户使用)
  • 我开发了一个DownloadHandler.ashx,当通过在这个域帐户下运行的进程进行POST时(我有一个带有定义PostBackUrl =“〜/ DownloadHandler.ashx”的按钮的.Net Web客户端)。这个HTTPHandler从HTTPContext中抓取一些项目,然后毫无问题地调用上面的webservice方法DownloadFile。

我的问题 现在,这个ColdFusion应用程序需要使用此Web服务下载文件。当CF代码将HTML表单发布到DownloadHandler.ashx时,它可以工作 - 但是只有CF测试者使用这个Windows域帐户。这在生产中不起作用,因为CF应用程序通过表单身份验证支持远程匿名用户。

问题

我自己不了解ColdFusion,我正在考虑以下变化:

  • 复制上述CF技术,使user / pswd可以设置为相同,并让CF直接调用ws.DownloadFile方法

  • 我认为这需要在我的webservice中使用我当前的大部分HTTPHandler代码,但我想不出如何处理输出。当这个处理程序被POST时,它会提示OPEN或Save并且工作得很好,但是我对如何从webservice本身传回来感到困惑。

  • 当前的DownloadFile webmethod与数据库产品通信并将输出返回给此(当前)处理程序:

代码

namespace WebClient
{
    public class DownloadHandler : IHttpHandler
    {
    ASMXproxy.FileService brokerService;
    public void ProcessRequest(HttpContext context)
    {
         brokerService = new ASMXproxy.FileService();
         string recNumber = context.Request.Form["txtRecordNumber"];
         brokerService.Url = context.Request.Form["txtURL"];
         string trimURL = context.Request.Form["txtFakeURLParm"];  // not a real URL but parms to connect to TRIM
         brokerService.Timeout = 9999999;
         brokerService.Credentials = System.Net.CredentialCache.DefaultCredentials;
         byte[] docContent;
         string fileType;
         string fileName;
         string msgInfo = brokerService.DownloadFile(trimURL, recNumber, out docContent, out fileType, out fileName);
         string ContentType = MIMEType.MimeType(fileType);
         context.Response.AppendHeader("Content-Length", docContent.Length.ToString());
         context.Response.AppendHeader("content-disposition", "attachment; filename=\"" + fileName + "\"");
         context.Response.ContentType = ContentType;
         context.Response.OutputStream.Write(docContent, 0, docContent.Length);
         context.Response.OutputStream.Flush();
     }
     public bool IsReusable
     {
         get
        {
             return false;
        }
     }
     }
}

1 个答案:

答案 0 :(得分:0)

假设您的CF站点在IIS而不是Apache或其他Web服务器上运行,这可能有效:

将调用webservice的.cfm文件放入您网站上自己的子文件夹中。将该文件夹的身份验证属性设置为使用匿名身份验证,但将用户身份设置为成功调用Web服务的Windows域帐户(单击下面显示的对话框上的设置...按钮并输入相应的凭据)。

IIS 7.5 Manager showing Anonymous Authentication