using System;
using System.Net;
using System.Diagnostics;
using System.IO;
namespace Test
{
public class ImageDown
{
public static void Main ()
{
Do ();
while (true) {
}
}
public static void Do ()
{
var url = "https://w...content-available-to-author-only...o.jp/intl/ja_ALL/images/logos/images_logo_lg.gif";
var req = (HttpWebRequest)WebRequest.Create (url);
req.BeginGetResponse (BeginReadCallBack, req);
}
class State
{
public Stream ResponseStream;
public MemoryStream MemoryStream = new MemoryStream ();
public byte[] Buffer = new byte[1024];
}
static void BeginReadCallBack (IAsyncResult ar)
{
var req = (HttpWebRequest)ar.AsyncState;
var response = (HttpWebResponse)req.EndGetResponse (ar);
var stream = response.GetResponseStream ();
var state = new State{ ResponseStream = stream };
stream.BeginRead (state.Buffer, 0, state.Buffer.Length, ReadCallback, state);
}
static void ReadCallback (IAsyncResult ar)
{
var state = (State)ar.AsyncState;
int readSize = state.ResponseStream.EndRead (ar);
if (readSize > 0) {
state.MemoryStream.Write (state.Buffer, 0, readSize);
state.ResponseStream.BeginRead (state.Buffer, 0, state.Buffer.Length, ReadCallback, state);
} else {
byte[] result = state.MemoryStream.GetBuffer ();
Debug.WriteLine ("Complete");
}
}
}
}
dXNpbmcgU3lzdGVtOwp1c2luZyBTeXN0ZW0uTmV0Owp1c2luZyBTeXN0ZW0uRGlhZ25vc3RpY3M7CnVzaW5nIFN5c3RlbS5JTzsKCm5hbWVzcGFjZSBUZXN0CnsKCXB1YmxpYyBjbGFzcyBJbWFnZURvd24KCXsKCQlwdWJsaWMgc3RhdGljIHZvaWQgTWFpbiAoKQoJCXsKCQkJRG8gKCk7CgkJCQoJCQl3aGlsZSAodHJ1ZSkgewoJCQl9CgkJfQoJCQoJCXB1YmxpYyBzdGF0aWMgdm9pZCBEbyAoKQoJCXsKCQkJdmFyIHVybCA9ICJodHRwczovL3cuLi5jb250ZW50LWF2YWlsYWJsZS10by1hdXRob3Itb25seS4uLm8uanAvaW50bC9qYV9BTEwvaW1hZ2VzL2xvZ29zL2ltYWdlc19sb2dvX2xnLmdpZi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