使用C#獲取遠(yuǎn)程圖片 Form用戶名與密碼Authorization認(rèn)證的實(shí)現(xiàn)
C#獲取遠(yuǎn)程圖片,需要Form用戶名和密碼的Authorization認(rèn)證
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Web.App_Code
{
public partial class GetFlexImage : System.Web.UI.Page
{
public static readonly log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
protected void Page_Load(object sender, EventArgs e)
{
if(Request["IMG"]==null||string.IsNullOrEmpty(Request["IMG"]))
{
return;
}
try
{
string url = (Request["IMG"]).Replace("%","%25");
HttpWebRequest WRequest;
HttpWebResponse response = null;
Uri uri = new Uri(url);
CredentialCache cc = new CredentialCache();
cc.Add(uri, "Basic", new NetworkCredential("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(new ASCIIEncoding().GetBytes("epapi:epapiadmin")));
try
{
response = (HttpWebResponse)WRequest.GetResponse();
}
catch (WebException er)
{
response = (HttpWebResponse)er.Response;
}
Bitmap myImage = new Bitmap(response.GetResponseStream());
MemoryStream ms = new MemoryStream();
myImage.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
Response.ClearContent();
Response.ContentType = "image/gif";
log.Debug("圖片加載:" + (Request["IMG"]));
Response.BinaryWrite(ms.ToArray());
}
catch(Exception err) {
log.Debug("圖片加載異常:" + Server.HtmlDecode(Request["IMG"]) + err.Message);
}
}
}
}
相關(guān)文章
基于Unity3D實(shí)現(xiàn)3D迷宮小游戲的示例代碼
迷宮游戲作為經(jīng)典的小游戲,一直深受大家的喜愛。本文小編將為大家詳細(xì)介紹一下如何用Unity實(shí)現(xiàn)一個(gè)3D版的迷宮小游戲,感興趣的可以動(dòng)手試一試2022-03-03C#在Windows窗體控件實(shí)現(xiàn)內(nèi)容拖放(DragDrop)功能
這篇文章介紹了C#在Windows窗體控件實(shí)現(xiàn)內(nèi)容拖放(DragDrop)的功能,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05C#基礎(chǔ)語法:結(jié)構(gòu)和類區(qū)別詳解
這篇文章主要介紹了C#基礎(chǔ)語法:結(jié)構(gòu)和類詳解,本文總結(jié)了一些結(jié)構(gòu)和類的不同之處并給出了測(cè)試區(qū)別特性代碼,需要的朋友可以參考下2015-06-06