如何从IHTTPActionResult

时间:2019-05-18 00:39:01

标签: vb.net

我有一个返回IHttpActionResult byteArray的api。调用API之后,我将文件保存到本地磁盘。打开文件时,它说无法打开文件,因为文件已损坏。似乎在使用API​​返回值时我做得不正确。您能告诉我您的建议吗?

1)返回IHTTPActionResult的API代码

            using (var resp = req.GetResponse() as HttpWebResponse)
            {
                using (Stream fs = resp.GetResponseStream())
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        int Count = 0;

                        do
                        {
                            byte[] Buffer = new byte[1025];

                            Count = fs.Read(Buffer, 0, 1024);
                            ms.Write(Buffer, 0, Count);
                        } while (fs.CanRead && Count > 0);

                        bytReturn = ms.ToArray();
                    }
                }
            }

            return Ok(bytReportResults);

2)调用API以获取HTTPActionResult字节数组

私有函数GetResponsByte(res as HttpWebResponse)as Byte()             昏暗的要求= WebRequest.CreateHttp(“ http://localhost/api/xxxx”)

        Using response = CType(req.GetResponse(), HttpWebResponse)
            If response.StatusCode = HttpStatusCode.OK Then
                Dim resourceContent As Byte() = Nothing
                If response IsNot Nothing Then
                    Dim binaryReader As System.IO.BinaryReader = New System.IO.BinaryReader(res.GetResponseStream())
                    resourceContent = binaryReader.ReadBytes(CInt(response.ContentLength))
                End If
            End If
        End Using
        Return resourceContent

    End Function

0 个答案:

没有答案