コード例 #1
0
        public void Download(string url, UInt32 requestId)
        {
            //判断文件是否存在
            string fileName = m_saveFolder + url.Substring(url.LastIndexOf("/") + 1);

            if (File.Exists(fileName))
            {
                Debug.WriteLine("File already download " + fileName);
                DownloadEventArgs args = new DownloadEventArgs();
                args.requestId = requestId;
                args.result    = fileName;
                DownloadEvent(this, args);
                return;
            }

            try
            {
                HttpWebRequest req       = CreateRequest(url, "GET");
                WebAsyncObject webObject = new WebAsyncObject();
                webObject.request   = req;
                webObject.requestId = requestId;
                webObject.webClient = this;
                req.BeginGetResponse(new AsyncCallback(onDownloadResponse), webObject);
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception.Message);
                DownloadEventArgs args = new DownloadEventArgs();
                args.requestId = requestId;
                args.result    = "";
                DownloadEvent(this, args);
            }
        }
コード例 #2
0
        public void Get(string url, UInt32 requestId)
        {
            //Debug.WriteLine("Get " + System.Threading.Thread.CurrentThread.ManagedThreadId);
            try
            {
                HttpWebRequest req = CreateRequest(url, "GET");

                WebAsyncObject webObject = new WebAsyncObject();
                webObject.request   = req;
                webObject.requestId = requestId;
                webObject.webClient = this;
                req.BeginGetResponse(new AsyncCallback(OnResponse), webObject);
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception.Message);
                DownloadEventArgs args = new DownloadEventArgs();
                args.requestId = requestId;
                args.result    = "";
                DownloadEvent(this, args);
            }
        }
コード例 #3
0
        public void Post(string url, string postData, UInt32 requestId)
        {
            //Debug.WriteLine("Post " + System.Threading.Thread.CurrentThread.ManagedThreadId);
            try
            {
                HttpWebRequest req = CreateRequest(url, "POST");
                byte[]         bs  = Encoding.ASCII.GetBytes(postData);
                req.ContentType   = "application/x-www-form-urlencoded";
                req.ContentLength = bs.Length;

                if (referer.Length > 0)
                {
                    req.Referer = referer;
                }
                req.Accept = "*/*";

                using (Stream reqStream = req.GetRequestStream())
                {
                    reqStream.Write(bs, 0, bs.Length);
                    reqStream.Close();
                }

                WebAsyncObject webObject = new WebAsyncObject();
                webObject.request   = req;
                webObject.requestId = requestId;
                webObject.webClient = this;

                req.BeginGetResponse(new AsyncCallback(OnResponse), webObject);
            }
            catch (Exception exception)
            {
                Debug.WriteLine(exception.Message);
                DownloadEventArgs args = new DownloadEventArgs();
                args.requestId = requestId;
                args.result    = "";
                DownloadEvent(this, args);
            }
        }