我可以从当前的httpcontext中进行异步发布吗?

时间:2019-04-16 17:53:02

标签: c# web-services dotnet-httpclient httpcontext

我试图从我的C#Web应用程序中调用Web服务,该Web服务基于我发送的某些参数来构建一个zip,然后将该zip作为下载返回给客户端。

工作流程如下:

  • 用户单击一个按钮,出现一个列出一些文件的对话框,然后他们选择所需的文件
  • 我们向另一个托管文件的网络服务器发起请求,将文件压缩并返回
  • 用户将zip文件下载到其下载文件夹中

我已经成功地实现了这一过程,但没有达到我想要的清晰或高效。该代码发布在下面。主要是代码不是异步的,我真的不喜欢需要将整个响应复制到当前上下文的事实。

public class GetZip : HttpTaskAsyncHandler
    {
        public override async Task ProcessRequestAsync(HttpContext context)
        {
            JavaScriptSerializer oSearlizer = new JavaScriptSerializer();

            object o = new
            {
                ...
            };

            string req = oSearlizer.Serialize(o);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:7071/api/get_zip");
            request.Method = "POST";
            request.ContentType = "application/json";
            using (Stream webStream = request.GetRequestStream())
            {
                using (StreamWriter streamWriter = new StreamWriter(webStream, Encoding.UTF8))
                {
                    streamWriter.Write(req);
                }
            }

            WebResponse webResponse = request.GetResponse();
            using (Stream webStream = webResponse.GetResponseStream())
            {
                if (webStream != null)
                {
                    using (BinaryReader responseReader = new BinaryReader(webStream))
                    {
                        byte[] byteResponse = ReadAllBytes(responseReader);
                        context.Response.Clear();
                        context.Response.Buffer = true;
                        context.Response.ContentType = "application/zip";
                        context.Response.AddHeader("content-disposition", "attachment;filename=test.zip"); // Save file         
                        context.Response.AddHeader("Content-Length", byteResponse.Length.ToString());
                        context.Response.Charset = "";
                        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
                        context.Response.BinaryWrite(byteResponse);
                        context.Response.End();
                    }
                }
            }
        }
    }

我正在调用的Web服务以以下方式返回数据:

HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                        response.Content = new ByteArrayContent(compressedBytes);
                        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                        response.Content.Headers.ContentDisposition.FileName = fileNameZip;
                        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");

我真正想做的就是直接从HttpContext发起另一个请求,以便将响应作为当前HttpContext的响应返回,而无需创建新请求,获取响应,然后将数据复制到当前HttpContext

类似这样的东西:

public class GetZip : HttpTaskAsyncHandler
    {
        public override async Task ProcessRequestAsync(HttpContext context)
        {
            JavaScriptSerializer oSearlizer = new JavaScriptSerializer();

            object o = new
            {
                ...
            };

            string req = oSearlizer.Serialize(o);
            HttpContent content = new StringContent(req, Encoding.UTF8, "application/json");
            HttpClient client = new HttpClient();

            await client.PostAsync("http://localhost:7071/api/get_zip", content);
        }

但是,这不会为客户端启动下载,因为当前HttpContext没有任何反应。有什么方法可以将当前的HttpContext附加到我正在创建的HttpClient上,因此响应将转到当前的上下文?还是有某种方法可以从当前的HttpClient中获得一个HttpContext

0 个答案:

没有答案