使用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)文章
WPF利用WindowChrome實(shí)現(xiàn)自定義窗口
這篇文章主要為大家詳細(xì)介紹了WPF如何利用WindowChrome實(shí)現(xiàn)自定義窗口,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下2023-02-02
WPF實(shí)現(xiàn)3D翻牌式倒計(jì)時(shí)特效
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)3D翻牌式倒計(jì)時(shí)特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
C#利用System.Uri轉(zhuǎn)URL為絕對(duì)地址的方法
這篇文章主要介紹了C#利用System.Uri轉(zhuǎn)URL為絕對(duì)地址的方法,涉及C#操作URL的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-02-02
C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音或轉(zhuǎn)拼音首字母的方法
這篇文章主要介紹了C#實(shí)現(xiàn)漢字轉(zhuǎn)拼音或轉(zhuǎn)拼音首字母的方法,涉及C#操作數(shù)組、遍歷及正則匹配的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
c++函數(shù)轉(zhuǎn)c#函數(shù)示例程序分享
這篇文章主要介紹了c++函數(shù)轉(zhuǎn)c#函數(shù)示例程序,大家參考使用吧2013-12-12
C#使用winform簡(jiǎn)單導(dǎo)出Excel的方法
這篇文章主要介紹了C#使用winform簡(jiǎn)單導(dǎo)出Excel的方法,結(jié)合實(shí)例形式分析了WinForm操作Excel文件的寫(xiě)入導(dǎo)出等相關(guān)技巧,需要的朋友可以參考下2016-06-06
C#編程自學(xué)之?dāng)?shù)據(jù)類型和變量三
C#語(yǔ)言類型系統(tǒng)提出的一個(gè)核心概念裝箱(boxing)拆箱(unboxing)。裝箱和取消裝箱的概念是C#的類型系統(tǒng)的核心。它在“值類型”和“引用類型”之間的架起了一座橋梁,使得任何“值類型”的值都可以轉(zhuǎn)換為object類型的值,反過(guò)來(lái)轉(zhuǎn)換也可以。2015-10-10

