使用C#获取远程图片Form用户名与密码Authorization认证的实现
C#获取远程图片,需要Form用户名和密码的Authorization认证
usingSystem;
usingSystem.Collections.Generic;
usingSystem.Drawing;
usingSystem.IO;
usingSystem.Linq;
usingSystem.Net;
usingSystem.Text;
usingSystem.Web;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
namespaceWeb.App_Code
{
publicpartialclassGetFlexImage:System.Web.UI.Page
{
publicstaticreadonlylog4net.ILoglog=log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
protectedvoidPage_Load(objectsender,EventArgse)
{
if(Request["IMG"]==null||string.IsNullOrEmpty(Request["IMG"]))
{
return;
}
try
{
stringurl=(Request["IMG"]).Replace("%","%25");
HttpWebRequestWRequest;
HttpWebResponseresponse=null;
Uriuri=newUri(url);
CredentialCachecc=newCredentialCache();
cc.Add(uri,"Basic",newNetworkCredential("epapi","密码"));
WRequest=(HttpWebRequest)HttpWebRequest.Create(uri);
WRequest.Credentials=cc;
WRequest.PreAuthenticate=true;
WRequest.Method="POST";
WRequest.AllowWriteStreamBuffering=false;
WRequest.SendChunked=false;
WRequest.KeepAlive=true;
WRequest.ContentLength=0;
//WRequest.SendChunked=true;
//WRequest.ContentLength=100000;
WRequest.Timeout=30000;
WRequest.Headers.Add("Authorization","Basic"+Convert.ToBase64String(newASCIIEncoding().GetBytes("epapi:epapiadmin")));
try
{
response=(HttpWebResponse)WRequest.GetResponse();
}
catch(WebExceptioner)
{
response=(HttpWebResponse)er.Response;
}
BitmapmyImage=newBitmap(response.GetResponseStream());
MemoryStreamms=newMemoryStream();
myImage.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType="image/gif";
log.Debug("图片加载:"+(Request["IMG"]));
Response.BinaryWrite(ms.ToArray());
}
catch(Exceptionerr){
log.Debug("图片加载异常:"+Server.HtmlDecode(Request["IMG"])+err.Message);
}
}
}
}
相关文章
- ADO数据库C#中ExecuteReader、ExecuteNonQuery、ExecuteScalar、SqlDataReader、SqlDataAdapter
- C# textbox密码框设置[通俗易懂]
- C#-TreeView
- Python表白C#?文末有彩蛋!
- C#使用正则表达式实例
- C#数据结构与算法揭秘四双向链表
- C#数据绑定控件中的DataSource属性浅谈
- C#zxing二维码写入的实例代码
- C#中is与As运算符号的使用详解
- 浅析C#web访问mysql数据库-整理归纳总结
- 通过C#动态生成图书信息XML文件
- C#WinForm导出Excel方法介绍
- c#实现把汉字转为带田字格背景的jpg图片
- C#中使用1.7版本驱动操作MongoDB简单例子
- C#使用伪随机数实现加密用户密码的方法
- C#调用Oracle存储过程的方法