本文介绍了将webresponse转换为字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在C#中运行以下代码:



WebResponse response = request.GetResponse();



我希望将结果存储/转换为字节数组。



请帮助我。



提前致谢。

Hi All,
I am running the following code in C#:

WebResponse response = request.GetResponse();

and I want to store/convert the result into the byte array.

Please help me.

Thanks in advance.

推荐答案

byte [] respBuffer = new byte[1024];
try
{
    int bytesRead = responseStream.Read(respbuffer,0,respBuffer.Length);
    if(bytesRead > 0)
    {
        memStream.Write(respBuffer,0,bytesRead);
        bytesRead = responseStream.Read(respBuffer,0,respBuffer.Length);
    }
}
catch(Exception ex)
{
    //Handle exception
}
finally
{
    responseStream.Close();
    webResponse.Close();
}





还可以查看 [] 。





--Amit



And also view the similar thread[^].


--Amit


using (Stream responseStream = request.GetResponse().GetResponseStream())
 {
byte[] buffer = new byte[0x1000];
int bytes;
while ((bytes = responseStream.Read(buffer, 0, buffer.Length)) > 0)
 {
memoryStream.Write(buffer, 0, bytes);
}
}


这篇关于将webresponse转换为字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-21 15:39