博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
获取图片为二进制流,并且显示图片到网页
阅读量:6716 次
发布时间:2019-06-25

本文共 1643 字,大约阅读时间需要 5 分钟。

protected void Page_Load(object sender, EventArgs e)

{
if (!IsPostBack)
{
LoadPage();
}
}
private void LoadPage()
{
string url = "http://app-server/wa/Plex/mossout/Preview.ashx?pid=52&id=5";
byte[] m_buffer = DownloadImage(url);
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(m_buffer);
}
#region 获取图片的二进制流
/// <summary>
/// 获取图片的二进制流
/// </summary>
/// <returns></returns>
public static byte[] DownloadImage(string url)
{

HttpWebRequest rqs = HttpWebRequest.Create(url)

as HttpWebRequest;
rqs.Credentials = CredentialCache.DefaultCredentials;
rqs.Credentials = CredentialCache.DefaultNetworkCredentials;
rqs.Accept = "*/*";
rqs.KeepAlive = true;
rqs.Method = "GET";
rqs.ContentType = "application/x-www-form-urlencoded";
rqs.CookieContainer = null;
rqs.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; InfoPath.3)";
rqs.Timeout = 50000;
System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
{ return true; };
HttpWebResponse hwResponse = rqs.GetResponse() as HttpWebResponse;
byte[] m_buffer = new byte[1024 * 1024];
Stream stream = hwResponse.GetResponseStream();
int offset = 0;
int count = 0;
do
{
count = stream.Read(m_buffer, offset, m_buffer.Length - offset);
if (count > 0)
{
offset += count;
}

} while (count > 0);

hwResponse.Close();
if (offset > 0)
{
byte[] ret = new byte[offset];
Array.Copy(m_buffer, ret, offset);
return ret;
}
else
{
return null;
}
}
#endregion

转载于:https://www.cnblogs.com/huaan011/p/3467655.html

你可能感兴趣的文章
【BZOJ】2151 种树
查看>>
hello Cookie
查看>>
xml2map map2xml
查看>>
Extra Credits: Symbolism 101
查看>>
Idea配置JRebel插件的详细配置及图解
查看>>
[LeetCode] #167# Two Sum II : 数组/二分查找/双指针
查看>>
29、SurfaceView
查看>>
QLExpress语法介绍
查看>>
[python] [转]如何自动生成和安装requirements.txt依赖
查看>>
java中微信统一下单采坑(app微信支付)
查看>>
AngularJs创建省,市,区的3级列表
查看>>
wp7 独立存储
查看>>
项目UML设计(团队)
查看>>
Divideing Jewels
查看>>
Oulipo (poj3461
查看>>
无法建立目录wp-content/uploads/xxxx/xx。有没有上级目录的写权限?解决办法
查看>>
poj 3037 Skiing
查看>>
BZOJ 2049 洞穴勘测
查看>>
洛谷P4169 天使玩偶 (算竞进阶习题)
查看>>
11周
查看>>