对象被设置PostAsync后与HttpClient的对象、PostAsync、HttpClient

2023-09-03 07:25:48 作者:执子之手、拖去喂狗

我想用HttpClient的,如果一些在接收端失败我要重新发送相同的文件流发送的文件。

我要创建一个MultipartFormDataContent,其中包含流POST请求。 一切看起来都很好,当我打电话PostAsync首次。但是,当我试图重复请求我得到System.ObjectDisposedException。

我的文件流布置PostAsync的第一次通话后...为什么,是有一个解决我的问题?

下面是什么我谈论基本的例子。

 公众的ActionResult指数()
    {
        VAR的客户=新的HttpClient {BaseAddress =新的URI(Request.Url.AbsoluteUri)};

        VAR网络=新的FileInfo(@C:\ json.zip);

        使用(VAR流= fi.OpenRead())
        {
            VAR内容=新MultipartFormDataContent();
            VAR streamContent =新StreamContent(流);
            streamContent.Headers.ContentType =新MediaTypeHeaderValue(应用程序/八位字节流);
            streamContent.Headers.ContentDisposition =新ContentDispositionHeaderValue(表单数据)
            {
                文件名=\文件\
            };

            content.Add(streamContent);

            VAR isSuccess = client.PostAsync(首页/认沽,内容)。
                ContinueWith(X => x.Result.Content.ReadAsAsync< JsonResponse>()Result.Success。)。结果;
            //流已经部署

            如果(!isSuccess)
            {
                isSuccess = client.PostAsync(首页/认沽,内容)。
                    ContinueWith(X => x.Result.Content.ReadAsAsync< JsonResponse>()Result.Success。)。结果;
            }
        }

        返回查看();
    }

    公共JsonResult认沽(HttpPostedFileBase文件)
    {
        返回JSON(新JsonResponse {成功= FALSE});
    }
 

解决方案 android async http框架的例子怎么导入工具运行

如果你调用LoadIntoBufferAsync Content对象上,将复制的文件流进StreamContent对象中一个MemoryStream。这样一来,在处理HttpContent不要关闭您的FileStream。您将需要重新定位流指针,并创建一个新的StreamContent使第二个电话。

I'm trying to send a file with HttpClient and if something on the receiving side fails I want to resend the same file stream.

I'm creating a post request with a MultipartFormDataContent, which contains the stream. Everything looks fine when I call PostAsync for the first time. But when I try to repeat the request I get System.ObjectDisposedException.

My file stream is disposed after the first call of PostAsync... Why and is there a solution to my problem?

Here is basic example of what am I talking about.

    public ActionResult Index()
    {
        var client = new HttpClient { BaseAddress = new Uri(Request.Url.AbsoluteUri) };

        var fi = new FileInfo(@"c:\json.zip");

        using (var stream = fi.OpenRead())
        {
            var content = new MultipartFormDataContent();
            var streamContent = new StreamContent(stream);
            streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            streamContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {
                FileName = "\"File\""
            };

            content.Add(streamContent);

            var isSuccess = client.PostAsync("Home/Put", content).
                ContinueWith(x => x.Result.Content.ReadAsAsync<JsonResponse>().Result.Success).Result;
            //stream is already disposed

            if (!isSuccess)
            {
                isSuccess = client.PostAsync("Home/Put", content).
                    ContinueWith(x => x.Result.Content.ReadAsAsync<JsonResponse>().Result.Success).Result;
            }
        }

        return View();
    }

    public JsonResult Put(HttpPostedFileBase file)
    {
        return Json(new JsonResponse { Success = false });
    }

解决方案

If you call LoadIntoBufferAsync on the Content object it will copy the file stream into a memorystream inside the StreamContent object. This way, disposing the HttpContent should not close your FileStream. You will need to reposition the stream pointer and create a new StreamContent to make the second call.