C # obtain the remote image. The Authorization authentication of the Form user name and password is required.
Copy codeThe Code is as follows: 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", "password "));
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. Time out = 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 ("image loading:" + (Request ["IMG"]);
Response. BinaryWrite (ms. ToArray ());
}
Catch (Exception err ){
Log. Debug ("image loading exception:" + Server. HtmlDecode (Request ["IMG"]) + err. Message );
}
}
}
}