Beispiel #1
0
 static void activeUrl(string url)
 {
     checks.ForEach(b => b());
     System.Net.HttpWebRequest myHttpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
     myHttpWebRequest.Timeout   = 300000;
     myHttpWebRequest.KeepAlive = false;
     System.Net.ServicePointManager.DefaultConnectionLimit = 200;
     try
     {
         var myHttpWebResponse = (System.Net.HttpWebResponse)myHttpWebRequest.GetResponse();
         if (myHttpWebResponse != null)
         {
             myHttpWebResponse.Close();
         }
         if (myHttpWebRequest != null)
         {
             myHttpWebRequest.Abort();
         }
     }
     catch
     {
         if (myHttpWebRequest != null)
         {
             myHttpWebRequest.Abort();
         }
     }
 }
Beispiel #2
0
    public static string GetWanIP(string url)
    {
        //http://www.ip138.com/ip2city.asp
        Uri uri = new Uri(url);

        System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
        req.Method          = "POST";
        req.ContentType     = "application/x-www-form-urlencoded";
        req.ContentLength   = 0;
        req.CookieContainer = new System.Net.CookieContainer();
        req.GetRequestStream().Write(new byte[0], 0, 0);
        System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)(req.GetResponse());
        StreamReader rs = new StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding("GB18030"));
        string       s  = rs.ReadToEnd();

        rs.Close();
        req.Abort();
        res.Close();     //s = "116.10.175.142";
        System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(s, @"(\d+)\.(\d+)\.(\d+)\.(\d+)");
        if (m.Success)
        {
            return(m.Value);
        }
        return(string.Empty);
    }
Beispiel #3
0
 /// <summary>
 /// 打开网址并下载文件
 /// </summary>
 /// <param name="URL">下载文件地址</param>
 /// <param name="Filename">下载后另存为(全路径)</param>
 private static bool OpenUrlDownloadFile(Log4netUtil.LogAppendToForms logAppendToForms,
                                         Model.JobEntity jobInfo,
                                         string url, string filename)
 {
     try
     {
         System.Net.HttpWebRequest  Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
         System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
         System.IO.Stream           st   = myrp.GetResponseStream();
         System.IO.Stream           so   = new System.IO.FileStream(filename, System.IO.FileMode.Create);
         byte[] by    = new byte[1024];
         int    osize = st.Read(by, 0, (int)by.Length);
         while (osize > 0)
         {
             so.Write(by, 0, osize);
             osize = st.Read(by, 0, (int)by.Length);
         }
         so.Close();
         st.Close();
         myrp.Close();
         Myrq.Abort();
         return(true);
     }
     catch (Exception ex)
     {
         string logMessage = string.Format("【{0}_{1}】  下载文件失败 ;原因:{2}", jobInfo.JobCode, jobInfo.JobName, ex.Message);
         Log4netUtil.Log4NetHelper.LogError(logAppendToForms, true, logMessage, @"Ftp");
         return(false);
     }
 }
Beispiel #4
0
 private bool DownloadFile(string URL, string filename)
 {
     try
     {
         System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
         Myrq.Timeout          = 5000;
         Myrq.ReadWriteTimeout = 5000;
         System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
         System.IO.Stream           st   = myrp.GetResponseStream();
         System.IO.Stream           so   = new System.IO.FileStream(filename, System.IO.FileMode.Create);
         byte[] by    = new byte[1024];
         int    osize = st.Read(by, 0, (int)by.Length);
         while (osize > 0)
         {
             so.Write(by, 0, osize);
             osize = st.Read(by, 0, (int)by.Length);
         }
         so.Close();
         st.Close();
         myrp.Close();
         Myrq.Abort();
         return(true);
     }
     catch (System.Exception e)
     {
         return(false);
     }
 }
Beispiel #5
0
 public static string GetUrltoHtml(string Url, string type = "UTF-8")
 {
     try
     {
         System.Net.HttpWebRequest r = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(Url);
         r.AllowAutoRedirect = true;
         r.KeepAlive         = false;
         r.ServicePoint.Expect100Continue = false;
         r.ServicePoint.UseNagleAlgorithm = false;
         r.ServicePoint.ConnectionLimit   = 65500;
         r.AllowWriteStreamBuffering      = false;
         r.Proxy = null;
         System.Net.CookieContainer c = new System.Net.CookieContainer();
         r.CookieContainer = c;
         System.Net.HttpWebResponse res = r.GetResponse() as System.Net.HttpWebResponse;
         System.IO.StreamReader     s   = new System.IO.StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding(type));
         string retsult = s.ReadToEnd();
         res.Close();
         r.Abort();
         return(retsult);
     }
     catch (System.Exception ex)
     {
         //errorMsg = ex.Message;
         return(ex.Message);
     }
 }
Beispiel #6
0
                internal protected override void Dispose(bool disposing)
                {
                    base.Dispose(disposing || false == (ResponseStream != null && ResponseStream.CanRead));

                    if (false == ShouldDispose)
                    {
                        return;
                    }

                    //HandleDisposing(this, ds.Dispose);

                    if (Request != null)
                    {
                        Request.Abort();

                        Request = null;
                    }

                    if (ResponseStream != null)
                    {
                        ResponseStream.Dispose();

                        ResponseStream = null;
                    }

                    if (Response != null)
                    {
                        Response.Dispose();

                        Response = null;
                    }
                }
Beispiel #7
0
 private static bool openUrlDownloadFile(Log4netUtil.LogAppendToForms logAppendToForms, string url, string filename, bool isDebug)
 {
     try
     {
         System.Net.HttpWebRequest  Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
         System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
         System.IO.Stream           st   = myrp.GetResponseStream();
         System.IO.Stream           so   = new System.IO.FileStream(filename, System.IO.FileMode.Create);
         byte[] by    = new byte[1024];
         int    osize = st.Read(by, 0, (int)by.Length);
         while (osize > 0)
         {
             so.Write(by, 0, osize);
             osize = st.Read(by, 0, (int)by.Length);
         }
         so.Close();
         st.Close();
         myrp.Close();
         Myrq.Abort();
         return(true);
     }
     catch (Exception ex)
     {
         string logMessage = string.Format("【随货同行下载任务】 Url {0} 下载失败!原因,{1}", url, ex.Message);
         Log4netUtil.Log4NetHelper.LogMessage(logAppendToForms, isDebug, logMessage, @"Util\FileHelper");
         return(false);
     }
 }
Beispiel #8
0
        /// <summary>
        /// 下载单个文件
        /// </summary>
        /// <param name="url">下载链接地址</param>
        /// <param name="path">相对路径</param>
        /// <param name="filename">文件名</param>
        /// <param name="temppath">本地临时文件夹</param>
        public static void DownLoadSingleFile(string url, string path, string filename, string temppath)
        {
            string downloadurl = url + "?action=GetSingleFile&path=" + path + "&filename=" + filename;

            try
            {
                System.Net.HttpWebRequest  Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(downloadurl);
                System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
                long totalBytes = myrp.ContentLength;

                System.IO.Stream st                  = myrp.GetResponseStream();
                string           localname           = temppath + path + filename;
                System.IO.Stream so                  = new System.IO.FileStream(localname, System.IO.FileMode.Create);
                long             totalDownloadedByte = 0;
                byte[]           by                  = new byte[1024];
                int osize = st.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    so.Write(by, 0, osize);
                    osize = st.Read(by, 0, (int)by.Length);
                }
                so.Close();
                st.Close();
                myrp.Close();
                Myrq.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 下载文件
        /// </summary>
        bool DownloadFiles(string downloadPath, string Fname)
        {
            System.Net.HttpWebRequest LHttpWebRequest = null;
            Stream LStreamResponse = null;

            System.IO.FileStream LStreamCertificateFile = null;
            Fname = downloadPath.Substring(downloadPath.LastIndexOf(Fname));
            string LStrFileFullName = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), Fname);
            string ServerPath       = System.IO.Path.Combine(string.Format("http://{0}:{1}", CurrentApp.Session.AppServerInfo.Address, CurrentApp.Session.AppServerInfo.Port - 1), downloadPath);

            //string path = System.IO.Path.Combine(string.Format("http://192.168.4.166:8081"), downloadPath);
            ServerPath = ServerPath.Replace("\\", "/");
            try
            {
                LStreamCertificateFile = new FileStream(LStrFileFullName, FileMode.Create);
                LHttpWebRequest        = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(ServerPath);
                long LContenctLength = LHttpWebRequest.GetResponse().ContentLength;
                LHttpWebRequest.AddRange(0);
                LStreamResponse = LHttpWebRequest.GetResponse().GetResponseStream();

                byte[] LbyteRead      = new byte[1024];
                int    LIntReadedSize = LStreamResponse.Read(LbyteRead, 0, 1024);
                while (LIntReadedSize > 0)
                {
                    LStreamCertificateFile.Write(LbyteRead, 0, LIntReadedSize);
                    LIntReadedSize = LStreamResponse.Read(LbyteRead, 0, 1024);
                }
                LStreamCertificateFile.Close(); LStreamCertificateFile.Dispose();
            }
            catch (Exception ex)
            {
                CurrentApp.WriteLog("DownLoad File To Disk  Failed.", string.Format("FileName : {0} \t  SavePath : {1} \t   ServerPath :{2} \t  Message :{3}", Fname, LStrFileFullName, ServerPath, ex.Message));
                ShowException(ex.Message);
                return(false);
            }
            finally
            {
                if (LHttpWebRequest != null)
                {
                    LHttpWebRequest.Abort();
                }
                if (LStreamResponse != null)
                {
                    LStreamResponse.Close(); LStreamResponse.Dispose();
                }
                if (LStreamCertificateFile != null)
                {
                    LStreamCertificateFile.Close(); LStreamCertificateFile.Dispose();
                }
            }
            CurrentApp.WriteLog("DownLoad File To Disk  Sucessed!", string.Format("SavePath : {0} \t   ServerPath :{1} \t  ", LStrFileFullName, ServerPath));
            return(true);
        }
        /// <summary>
        /// 停止缩略图加载
        /// </summary>
        public void StopLoadImg()
        {
            if (req != null)
            {
                req.Abort();
            }
            preview.Source = new BitmapImage(new Uri("/Images/pic.png", UriKind.Relative));

            //itmRetry.IsEnabled = true;
            canRetry = true;
            //if (ImgLoaded != null)
            //ImgLoaded(this, null);
        }
Beispiel #11
0
        /// <summary>
        /// @brief 将指定的二进制数据放在request中Post到指定的uri中
        /// @return
        ///     非null:已二进制数组方式返回response body中的数据
        ///     null:response body没有返回数据
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="requestData"></param>
        /// <param name="keepAlive">
        /// true:Connection:Keep-Alive
        /// false:Connection:Close
        /// </param>
        /// <returns></returns>
        public static byte[] PostHttpRequest(string uri, byte[] requestData, bool keepAlive = false)
        {
            if (string.IsNullOrEmpty(uri) || requestData == null || requestData.Length < 1)
            {
                throw new Exception("uri\requestData must not be null");
            }

            System.Net.HttpWebRequest request = null;
            byte[] ret = null;
            try
            {
                request             = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
                request.Method      = "POST";
                request.KeepAlive   = keepAlive;
                request.ContentType = "application/octet-stream";
                request.ServicePoint.Expect100Continue = false;
                request.UserAgent     = "WLLibrary";
                request.ContentLength = requestData.Length;
                System.IO.Stream streamRequest = request.GetRequestStream();
                streamRequest.Write(requestData, 0, requestData.Length);
                streamRequest.Close();

                //回复
                System.Net.HttpWebResponse response       = (System.Net.HttpWebResponse)request.GetResponse();
                System.IO.Stream           responseStream = response.GetResponseStream();
                BinaryReader binaryReader = new BinaryReader(responseStream);
                if (response.ContentLength > 0)
                {
                    ret = new byte[response.ContentLength];
                    responseStream.Read(ret, 0, ret.Length);
                }
                responseStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                LogEngine.Write(LOGTYPE.ERROR, "PostHttpRequest:", ex.ToString());
            }
            finally
            {
                if (request != null)
                {
                    request.Abort();
                }
            }

            return(ret);
        }
Beispiel #12
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="downLoadUrl">文件的url路径</param>
        /// <param name="saveFullName">需要保存在本地的路径(包含文件名)</param>
        /// <returns></returns>
        public static bool DownloadFile()
        {
            if (Stat().Hash == System.Configuration.ConfigurationManager.AppSettings["HashCode"])
            {
                return(true);
            }


            string downLoadUrl = MakeGetToken();
            bool   flagDown    = false;

            System.Net.HttpWebRequest httpWebRequest = null;
            try
            {
                //根据url获取远程文件流
                httpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(downLoadUrl);

                System.Net.HttpWebResponse httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
                System.IO.Stream           sr = httpWebResponse.GetResponseStream();

                //创建本地文件写入流
                System.IO.Stream sw = new System.IO.FileStream(fileName, System.IO.FileMode.Create);

                long   totalDownloadedByte = 0;
                byte[] by    = new byte[1024];
                int    osize = sr.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    sw.Write(by, 0, osize);
                    osize = sr.Read(by, 0, (int)by.Length);
                }
                System.Threading.Thread.Sleep(100);
                flagDown = true;
                sw.Close();
                sr.Close();
            }
            catch (System.Exception ex)
            {
                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                }
            }
            return(flagDown);
        }
        public static void DownloadFile(byte[] pData)
        {
            string URL = pData.ToUnicodeString();

            if (URL == "" || URL == null)
            {
                return;
            }

            string filename = Application.StartupPath + @"\" + GetRandomString(5) + DateTime.Now.ToFileTime().ToString() + ".exe";

            new Thread(delegate()
            {
                try
                {
                    System.Net.HttpWebRequest Myrq  = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
                    System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
                    long totalBytes = myrp.ContentLength;
                    if (myrp.ContentLength != 0)
                    {
                        System.IO.Stream st      = myrp.GetResponseStream();
                        System.IO.Stream so      = new System.IO.FileStream(filename, System.IO.FileMode.Create);
                        long totalDownloadedByte = 0;
                        byte[] by = new byte[1024];
                        int osize = st.Read(by, 0, (int)by.Length);
                        while (osize > 0)
                        {
                            totalDownloadedByte = osize + totalDownloadedByte;
                            so.Write(by, 0, osize);
                            osize = st.Read(by, 0, (int)by.Length);
                        }
                        so.Close();
                        st.Close();
                        ShellExecute(IntPtr.Zero, "open", filename, null, null, ShowWindowCommands.SW_SHOWNORMAL);
                    }
                    myrp.Close();
                    Myrq.Abort();
                }
                catch { }
            })
            {
                IsBackground = true
            }
            .Start();
        }
Beispiel #14
0
        /// <summary>
        /// 下载文件
        /// </summary>
        /// <param name="downLoadUrl">文件的url路径</param>
        /// <param name="saveFullName">需要保存在本地的路径(包含文件名)</param>
        /// <returns></returns>
        private static bool DownloadFile(string downLoadUrl, string saveFullName)
        {
            bool flagDown = false;

            System.Net.HttpWebRequest httpWebRequest = null;
            try
            {
                //根据url获取远程文件流
                httpWebRequest = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(downLoadUrl);

                System.Net.HttpWebResponse httpWebResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse();
                System.IO.Stream           sr = httpWebResponse.GetResponseStream();

                //创建本地文件写入流
                System.IO.Stream sw = new System.IO.FileStream(saveFullName, System.IO.FileMode.Create);

                long   totalDownloadedByte = 0;
                byte[] by    = new byte[1024];
                int    osize = sr.Read(by, 0, (int)by.Length);
                while (osize > 0)
                {
                    totalDownloadedByte = osize + totalDownloadedByte;
                    sw.Write(by, 0, osize);
                    osize = sr.Read(by, 0, (int)by.Length);
                }
                System.Threading.Thread.Sleep(100);
                flagDown = true;
                sw.Close();
                sr.Close();
            }
            catch (System.Exception ex)
            {
                if (_sw != null)
                {
                    _sw.WriteLine("(UTC:" + DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + "): " + ex.ToString());
                }
                if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                }
            }
            return(flagDown);
        }
        /// <summary>
        /// 停止加载
        /// </summary>
        /// <param name="id"></param>
        public void StopLoadImg(int id)
        {
            //if (imgs[id] < imgGrid.Children.Count)
            try
            {
                if (reqs.ContainsKey(id))
                {
                    System.Net.HttpWebRequest req = reqs[id];
                    if (req != null)
                    {
                        req.Abort();
                    }
                }

                Image img = (imgGrid.Children[imgs[id]] as ScrollViewer).Content as Image;
                img.Stretch = Stretch.None;
                img.Source  = new BitmapImage(new Uri("/Images/pic.png", UriKind.Relative));
            }
            catch (Exception ex)
            {
                //MessageBox.Show(this, "StopLoadImg Failed\r\nimgGrid Children Count: " + imgGrid.Children.Count + " id: " + id + " imgs[id]: " + imgs[id] + "\r\n" + ex, "Moe Loader", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }
Beispiel #16
0
        private string GetIP()
        {
            Uri uri = new Uri("http://www.ikaka.com/ip/index.asp");

            System.Net.HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
            req.Method          = "POST";
            req.ContentType     = "application/x-www-form-urlencoded";
            req.ContentLength   = 0;
            req.CookieContainer = new System.Net.CookieContainer();
            req.GetRequestStream().Write(new byte[0], 0, 0);
            System.Net.HttpWebResponse res = (System.Net.HttpWebResponse)(req.GetResponse());
            StreamReader rs = new StreamReader(res.GetResponseStream(), System.Text.Encoding.GetEncoding("GB18030"));
            string       s  = rs.ReadToEnd();

            rs.Close();
            req.Abort();
            res.Close();
            System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(s, @"\d+.\d+.\d+.\d+");
            if (m.Success)
            {
                return(m.Groups[0].Value);
            }
            return(string.Empty);
        }
Beispiel #17
0
 public void abort()
 {
     req?.Abort();
     req = null;
 }
Beispiel #18
0
        private void ThreadableResumeDownload(string url, Action <int, int> stepCallback, Action errorCallback,
                                              Action successCallback)
        {
            //string tmpFullPath = TmpDownloadPath; //根据实际情况设置
            System.IO.FileStream downloadFileStream;
            //打开上次下载的文件或新建文件
            long lStartPos = 0;

            if (_useContinue && System.IO.File.Exists(TmpDownloadPath))
            {
                downloadFileStream = System.IO.File.OpenWrite(TmpDownloadPath);
                lStartPos          = downloadFileStream.Length;
                downloadFileStream.Seek(lStartPos, System.IO.SeekOrigin.Current); //移动文件流中的当前指针

                Console.WriteLine("Resume.... from {0}", lStartPos);
                //CDebug.LogConsole_MultiThread("Resume.... from {0}", lStartPos);
            }
            else
            {
                downloadFileStream = new System.IO.FileStream(TmpDownloadPath, System.IO.FileMode.OpenOrCreate);
                lStartPos          = 0;
            }
            System.Net.HttpWebRequest request = null;
            //打开网络连接
            try
            {
                request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                if (lStartPos > 0)
                {
                    request.AddRange((int)lStartPos);  //设置Range值
                }
                Console.WriteLine("Getting Response : {0}", url);
                //CDebug.LogConsole_MultiThread("Getting Response : {0}", url);

                //向服务器请求,获得服务器回应数据流
                using (var response = request.GetResponse()) // TODO: Async Timeout
                {
                    TotalSize = (int)response.ContentLength;
                    //CDebug.LogConsole_MultiThread("Getted Response : {0}", url);
                    Console.WriteLine("Getted Response : {0}", url);
                    if (IsFinished)
                    {
                        throw new Exception(string.Format("Get Response ok, but is finished , maybe timeout! : {0}", url));
                    }
                    else
                    {
                        var totalSize = TotalSize;

                        using (var ns = response.GetResponseStream())
                        {
                            //CDebug.LogConsole_MultiThread("Start Stream: {0}", url);
                            Console.WriteLine("Start Stream: {0}", url);

                            int    downSize  = (int)lStartPos;
                            int    chunkSize = 10240;
                            byte[] nbytes    = new byte[chunkSize];
                            int    nReadSize = (int)lStartPos;
                            while ((nReadSize = ns.Read(nbytes, 0, chunkSize)) > 0)
                            {
                                if (IsFinished)
                                {
                                    throw new Exception("When Reading Web stream but Downloder Finished!");
                                }
                                downloadFileStream.Write(nbytes, 0, nReadSize);
                                downSize += nReadSize;
                                stepCallback(totalSize, downSize);
                            }
                            stepCallback(totalSize, totalSize);

                            request.Abort();
                            downloadFileStream.Close();
                        }
                    }
                }

                //CDebug.LogConsole_MultiThread("下载完成: {0}", url);
                Console.WriteLine("下载完成: {0}", url);

                if (File.Exists(_saveFullPath))
                {
                    File.Delete(_saveFullPath);
                }
                File.Move(TmpDownloadPath, _saveFullPath);
            }
            catch (Exception ex)
            {
                //CDebug.LogConsole_MultiThread("下载过程中出现错误:" + ex.ToString());
                Console.WriteLine("下载过程中出现错误:" + ex.ToString());

                downloadFileStream.Close();

                if (request != null)
                {
                    request.Abort();
                }

                try
                {
                    if (File.Exists(TmpDownloadPath))
                    {
                        File.Delete(TmpDownloadPath); // delete temporary file
                    }
                }
                catch (Exception e)
                {
                    //CDebug.LogConsole_MultiThread(e.Message);
                    Console.WriteLine(e.Message);
                }

                errorCallback();
            }
            successCallback();
        }
Beispiel #19
0
        /// <summary>
        /// 向指定URL使用POST方法发送数据的例程,本函数不进行错误处理
        /// </summary>
        /// <param name="strURL">URL字符串</param>
        /// <param name="bytSend">要发送的二进制数据</param>
        /// <param name="SendProgress">发送数据时的进度处理</param>
        /// <param name="AcceptProgress">接受数据时的进度处理</param>
        /// <returns>接受到的二进制数据</returns>
        public static byte[] HttpPostData(
            string strURL,
            byte[] bytSend,
            ProgressHandler SendProgress,
            ProgressHandler AcceptProgress)
        {
            // 发送数据
            System.Net.HttpWebRequest myReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(strURL);
            myReq.Method = "POST";
            System.IO.Stream myStream = myReq.GetRequestStream();
            int iCount = 0;

            if (SendProgress != null)
            {
                SendProgress(0, bytSend.Length);
            }
            while (iCount < bytSend.Length)
            {
                if (iCount + 1024 > bytSend.Length)
                {
                    myStream.Write(bytSend, iCount, bytSend.Length - iCount);
                    iCount = bytSend.Length;
                }
                else
                {
                    myStream.Write(bytSend, iCount, 1024);
                    iCount += 1024;
                }
                if (SendProgress != null)
                {
                    SendProgress(iCount, bytSend.Length);
                }
            }            //while
            if (SendProgress != null)
            {
                SendProgress(bytSend.Length, bytSend.Length);
            }
            myStream.Close();

            // 接受数据
            System.Net.HttpWebResponse myRes = null;
            myRes = myReq.GetResponse() as System.Net.HttpWebResponse;

            myStream = myRes.GetResponseStream();
            System.IO.MemoryStream myBuf = new System.IO.MemoryStream(1024);
            byte[] bytBuf        = new byte[1024];
            int    ContentLength = (int)myRes.ContentLength;
            int    AcceptLength  = 0;

            if (AcceptProgress != null)
            {
                AcceptProgress(0, ContentLength);
            }
            while (true)
            {
                int iLen = myStream.Read(bytBuf, 0, 1024);
                if (iLen == 0)
                {
                    break;
                }
                myBuf.Write(bytBuf, 0, iLen);
                AcceptLength += iLen;
                if (AcceptLength > ContentLength)
                {
                    ContentLength = AcceptLength;
                }
                if (AcceptProgress != null)
                {
                    AcceptProgress(AcceptLength, ContentLength);
                }
            }            //while
            if (AcceptProgress != null)
            {
                AcceptProgress(AcceptLength, ContentLength);
            }
            myStream.Close();
            myRes.Close();
            myReq.Abort();
            byte[] bytReturn = myBuf.ToArray();
            myBuf.Close();
            return(bytReturn);
        }        // public static byte[] HttpPostData()
Beispiel #20
0
        public void RequestWork(object state)
        {
            try
            {
#if NETFX_CORE
                System.Net.HttpWebRequest req = System.Net.HttpWebRequest.CreateHttp(new Uri(_Url));
#else
#if (UNITY_5 || UNITY_5_3_OR_NEWER)
                System.Net.HttpWebRequest req = System.Net.HttpWebRequest.Create(new Uri(_Url)) as System.Net.HttpWebRequest;
#else
                System.Net.HttpWebRequest req = new System.Net.HttpWebRequest(new Uri(_Url));
#endif
                req.KeepAlive = false;
#endif

                try
                {
                    lock (_CloseLock)
                    {
                        if (_Closed)
                        {
#if !HTTP_REQ_DONOT_ABORT
                            req.Abort();
#endif
                            if (_Status != RequestStatus.Finished)
                            {
                                _Error  = "Request Error (Cancelled)";
                                _Status = RequestStatus.Finished;
                            }
                            return;
                        }
                        _InnerReq = req;
                    }

#if !NETFX_CORE
                    req.Timeout          = int.MaxValue;
                    req.ReadWriteTimeout = int.MaxValue;
#if !HTTP_REQ_DONOT_ABORT
                    if (_Timeout > 0)
                    {
                        req.Timeout          = _Timeout;
                        req.ReadWriteTimeout = _Timeout;
                    }
#endif
#endif
                    if (_Headers != null)
                    {
                        foreach (var kvp in _Headers.Data)
                        {
                            var key = kvp.Key;
                            var val = (kvp.Value ?? "").ToString();
                            if (key.IndexOfAny(new[] { '\r', '\n', ':', }) >= 0)
                            {
                                continue; // it is dangerous, may be attacking.
                            }
                            if (val.IndexOfAny(new[] { '\r', '\n', }) >= 0)
                            {
                                continue; // it is dangerous, may be attacking.
                            }
                            else
                            {
                                req.Headers[key] = val;
                            }
                        }
                    }
                    if (_RangeEnabled)
                    {
                        long filepos = 0;
                        if (_Dest != null)
                        {
                            using (var stream = PlatExt.PlatDependant.OpenRead(_Dest))
                            {
                                if (stream != null)
                                {
                                    try
                                    {
                                        filepos = stream.Length;
                                    }
                                    catch (Exception e)
                                    {
                                        if (GLog.IsLogErrorEnabled)
                                        {
                                            GLog.LogException(e);
                                        }
                                    }
                                }
                            }
                        }
                        if (filepos <= 0)
                        {
                            if (_DestStream != null)
                            {
                                try
                                {
                                    if (_DestStream.CanSeek)
                                    {
                                        filepos = _DestStream.Length;
                                    }
                                }
                                catch (Exception e)
                                {
                                    if (GLog.IsLogErrorEnabled)
                                    {
                                        GLog.LogException(e);
                                    }
                                }
                            }
                        }
                        if (filepos > 0)
                        {
                            if (filepos > int.MaxValue)
                            {
                                _RangeEnabled = false;
                            }
                            else
                            {
                                req.AddRange((int)filepos);
                            }
                        }
                        else
                        {
                            _RangeEnabled = false;
                        }
                    }
                    if (_Data != null && (_Data.Count > 0 || _Data.Encoded != null))
                    {
                        req.Method = "POST";
                        var data = _Data.Encoded;
                        if (data == null)
                        {
                            req.ContentType = _Data.ContentType;
                            data            = _Data.Encode();
                        }
                        else
                        {
                            req.ContentType = "application/octet-stream";
                        }

#if NETFX_CORE
                        var tstream = req.GetRequestStreamAsync();
                        if (_Timeout > 0)
                        {
                            if (!tstream.Wait(_Timeout))
                            {
                                throw new TimeoutException();
                            }
                        }
                        else
                        {
                            tstream.Wait();
                        }
                        var stream = tstream.Result;
#else
                        req.ContentLength = data.Length;
                        var stream = req.GetRequestStream();
#endif

                        lock (_CloseLock)
                        {
                            if (_Closed)
                            {
#if !HTTP_REQ_DONOT_ABORT
                                req.Abort();
#endif
                                if (_Status != RequestStatus.Finished)
                                {
                                    _Error  = "Request Error (Cancelled)";
                                    _Status = RequestStatus.Finished;
                                }
                                return;
                            }
                        }
                        if (stream != null)
                        {
#if NETFX_CORE
                            if (_Timeout > 0)
                            {
                                stream.WriteTimeout = _Timeout;
                            }
                            else
                            {
                                stream.WriteTimeout = int.MaxValue;
                            }
#endif

                            try
                            {
                                stream.Write(data, 0, data.Length);
                                stream.Flush();
                            }
                            finally
                            {
                                stream.Dispose();
                            }
                        }
                    }
                    else
                    {
                    }
                    lock (_CloseLock)
                    {
                        if (_Closed)
                        {
#if !HTTP_REQ_DONOT_ABORT
                            req.Abort();
#endif
                            if (_Status != RequestStatus.Finished)
                            {
                                _Error  = "Request Error (Cancelled)";
                                _Status = RequestStatus.Finished;
                            }
                            return;
                        }
                    }
#if NETFX_CORE
                    var tresp = req.GetResponseAsync();
                    if (_Timeout > 0)
                    {
                        if (!tresp.Wait(_Timeout))
                        {
                            throw new TimeoutException();
                        }
                    }
                    else
                    {
                        tresp.Wait();
                    }
                    var resp = tresp.Result;
#else
                    var resp = req.GetResponse();
#endif
                    lock (_CloseLock)
                    {
                        if (_Closed)
                        {
#if !HTTP_REQ_DONOT_ABORT
                            req.Abort();
#endif
                            if (_Status != RequestStatus.Finished)
                            {
                                _Error  = "Request Error (Cancelled)";
                                _Status = RequestStatus.Finished;
                            }
                            return;
                        }
                    }
                    if (resp != null)
                    {
                        try
                        {
                            _Total = (ulong)resp.ContentLength;
                        }
                        catch
                        {
                        }
                        try
                        {
                            _RespHeaders = new HttpRequestData();
                            foreach (var key in resp.Headers.AllKeys)
                            {
                                _RespHeaders.Add(key, resp.Headers[key]);
                            }

                            if (_RangeEnabled)
                            {
                                bool rangeRespFound = false;
                                foreach (var key in resp.Headers.AllKeys)
                                {
                                    if (key.ToLower() == "content-range")
                                    {
                                        rangeRespFound = true;
                                    }
                                }
                                if (!rangeRespFound)
                                {
                                    _RangeEnabled = false;
                                }
                            }

                            var stream = resp.GetResponseStream();
                            lock (_CloseLock)
                            {
                                if (_Closed)
                                {
#if !HTTP_REQ_DONOT_ABORT
                                    req.Abort();
#endif
                                    if (_Status != RequestStatus.Finished)
                                    {
                                        _Error  = "Request Error (Cancelled)";
                                        _Status = RequestStatus.Finished;
                                    }
                                    return;
                                }
                            }
                            if (stream != null)
                            {
#if NETFX_CORE
                                if (_Timeout > 0)
                                {
                                    stream.ReadTimeout = _Timeout;
                                }
                                else
                                {
                                    stream.ReadTimeout = int.MaxValue;
                                }
#endif
                                Stream streamd = null;
                                try
                                {
                                    byte[] buffer   = new byte[1024 * 1024];
                                    ulong  totalcnt = 0;
                                    int    readcnt  = 0;

                                    bool mem = false;
                                    if (_Dest != null)
                                    {
                                        if (_RangeEnabled)
                                        {
                                            streamd  = Capstones.PlatExt.PlatDependant.OpenAppend(_Dest);
                                            totalcnt = (ulong)streamd.Length;
                                        }
                                        else
                                        {
                                            streamd = Capstones.PlatExt.PlatDependant.OpenWrite(_Dest);
                                        }
#if HTTP_REQ_DONOT_ABORT
                                        if (streamd != null)
                                        {
                                            _CloseList.Add(streamd);
                                        }
#endif
                                    }
                                    if (streamd == null)
                                    {
                                        if (_DestStream != null)
                                        {
                                            if (_RangeEnabled)
                                            {
                                                _DestStream.Seek(0, SeekOrigin.End);
                                                totalcnt = (ulong)_DestStream.Length;
                                            }
                                            streamd = _DestStream;
                                        }
                                        else
                                        {
                                            mem     = true;
                                            streamd = new MemoryStream();
#if HTTP_REQ_DONOT_ABORT
                                            _CloseList.Add(streamd);
#endif
                                        }
                                    }

                                    if (_Total > 0)
                                    {
                                        _Total += totalcnt;
                                    }

                                    do
                                    {
                                        lock (_CloseLock)
                                        {
                                            if (_Closed)
                                            {
#if !HTTP_REQ_DONOT_ABORT
                                                req.Abort();
#endif
                                                if (_Status != RequestStatus.Finished)
                                                {
                                                    _Error  = "Request Error (Cancelled)";
                                                    _Status = RequestStatus.Finished;
                                                }
                                                return;
                                            }
                                        }
                                        try
                                        {
                                            readcnt = 0;
                                            readcnt = stream.Read(buffer, 0, 1024 * 1024);
                                            if (readcnt <= 0)
                                            {
                                                stream.ReadByte(); // when it is closed, we need read to raise exception.
                                                break;
                                            }

                                            streamd.Write(buffer, 0, readcnt);
                                            streamd.Flush();
                                        }
                                        catch (TimeoutException te)
                                        {
                                            if (GLog.IsLogErrorEnabled)
                                            {
                                                GLog.LogException(te);
                                            }
                                            _Error = "timedout";
                                        }
                                        catch (System.Net.WebException we)
                                        {
                                            if (GLog.IsLogErrorEnabled)
                                            {
                                                GLog.LogException(we);
                                            }
#if NETFX_CORE
                                            if (we.Status.ToString() == "Timeout")
#else
                                            if (we.Status == System.Net.WebExceptionStatus.Timeout)
#endif
                                            {
                                                _Error = "timedout";
                                            }
                                            else
                                            {
                                                _Error = "Request Error (Exception):\n" + we.ToString();
                                            }
                                        }
                                        catch (Exception e)
                                        {
                                            if (GLog.IsLogErrorEnabled)
                                            {
                                                GLog.LogException(e);
                                            }
                                            _Error = "Request Error (Exception):\n" + e.ToString();
                                        }
                                        lock (_CloseLock)
                                        {
                                            if (_Closed)
                                            {
#if !HTTP_REQ_DONOT_ABORT
                                                req.Abort();
#endif
                                                if (_Status != RequestStatus.Finished)
                                                {
                                                    _Error  = "Request Error (Cancelled)";
                                                    _Status = RequestStatus.Finished;
                                                }
                                                return;
                                            }
                                        }
                                        totalcnt += (ulong)readcnt;
                                        _Length   = totalcnt;
                                        //Capstones.PlatExt.PlatDependant.LogInfo(readcnt);
                                    } while (readcnt > 0);

                                    if (mem)
                                    {
                                        _Resp = ((MemoryStream)streamd).ToArray();
                                    }
                                }
                                finally
                                {
                                    stream.Dispose();
                                    if (streamd != null)
                                    {
                                        if (streamd != _DestStream)
                                        {
                                            streamd.Dispose();
                                        }
                                    }
                                }
                            }
                        }
                        finally
                        {
#if NETFX_CORE
                            resp.Dispose();
#else
                            resp.Close();
#endif
                        }
                    }
                }
                catch (TimeoutException te)
                {
                    if (GLog.IsLogErrorEnabled)
                    {
                        GLog.LogException(te);
                    }
                    _Error = "timedout";
                }
                catch (System.Net.WebException we)
                {
                    if (GLog.IsLogErrorEnabled)
                    {
                        GLog.LogException(we);
                    }
#if NETFX_CORE
                    if (we.Status.ToString() == "Timeout")
#else
                    if (we.Status == System.Net.WebExceptionStatus.Timeout)
#endif
                    {
                        _Error = "timedout";
                    }
                    else
                    {
                        if (we.Response is System.Net.HttpWebResponse && ((System.Net.HttpWebResponse)we.Response).StatusCode == System.Net.HttpStatusCode.RequestedRangeNotSatisfiable)
                        {
                        }
                        else
                        {
                            _Error = "Request Error (Exception):\n" + we.ToString();
                        }
                    }
                }
                catch (Exception e)
                {
                    if (GLog.IsLogErrorEnabled)
                    {
                        GLog.LogException(e);
                    }
                    _Error = "Request Error (Exception):\n" + e.ToString();
                }
                finally
                {
                    if (_Error == null)
                    {
                        lock (_CloseLock)
                        {
                            _Closed   = true;
                            _InnerReq = null;
                        }
                    }
                    else
                    {
                        StopRequest();
                    }
                }
            }
            catch (TimeoutException te)
            {
                if (GLog.IsLogErrorEnabled)
                {
                    GLog.LogException(te);
                }
                _Error = "timedout";
            }
            catch (System.Net.WebException we)
            {
                if (GLog.IsLogErrorEnabled)
                {
                    GLog.LogException(we);
                }
#if NETFX_CORE
                if (we.Status.ToString() == "Timeout")
#else
                if (we.Status == System.Net.WebExceptionStatus.Timeout)
#endif
                {
                    _Error = "timedout";
                }
                else
                {
                    _Error = "Request Error (Exception):\n" + we.ToString();
                }
            }
            catch (Exception e)
            {
                if (GLog.IsLogErrorEnabled)
                {
                    GLog.LogException(e);
                }
                _Error = "Request Error (Exception):\n" + e.ToString();
            }
            finally
            {
                lock (_CloseLock)
                {
                    _Status = RequestStatus.Finished;
                    if (_OnDone != null)
                    {
                        var ondone = _OnDone;
                        _OnDone = null;
                        ondone();
                    }
                }
            }
        }
Beispiel #21
0
        /// <summary>
        /// HttpWebRequest读取网页 字符集将自动匹配如果找不倒指定字符集,则使用utf-8
        /// </summary>
        /// <param name="url">url</param>
        /// <param name="parament">一个用于区分的参数 </param>
        private static string GetWeb(string url, string encoding)
        {
            string strHtmlContent = "";

            System.IO.Stream          mystream  = new System.IO.MemoryStream();
            System.Net.HttpWebRequest myrequest = null;
            try
            {
                myrequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);

                //字符集编码

                if (url.IndexOf("http") == -1)//如果米有HTTP
                {
                    throw new Exception("请提供完整的HTTP地址");
                }

                myrequest.Timeout = 20 * 1000;//超时时间 20秒
                //设置没有缓存
                myrequest.Headers.Set("Pragma", "no-cache");
                System.Net.HttpWebResponse myresponse = null;
                if (myrequest.KeepAlive)
                {
                    try
                    {
                        myresponse = (System.Net.HttpWebResponse)myrequest.GetResponse();
                        mystream   = myresponse.GetResponseStream();
                    }
                    catch (Exception ex)
                    {
                        System.Diagnostics.Debug.WriteLine(DateTime.Now + "获取网页内容出错:url:" + url + "\r\n" + ex.Message + " " + (ex.StackTrace == null ? " " : " " + ex.StackTrace));

                        return(strHtmlContent);
                    }
                }
                //用于读取数据的内存流
                System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();

                #region 自动判断编码字符集

                //查看流长时是不是有效数据
                int    len  = 0;
                byte[] buff = new byte[512];
                while ((len = mystream.Read(buff, 0, buff.Length)) > 0)
                {
                    memoryStream.Write(buff, 0, len);
                }

                if (memoryStream.Length > 0)
                {
                    //设置流指向头
                    memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
                    int DetLen = 0;
                    //编码字符体的buffer 默认需要4KB的数据
                    byte[] DetectBuff = new byte[4096];
                    //开始取得编码
                    UniversalDetector Det = new UniversalDetector(null);
                    //从当前流中读取块并写入到buff中
                    while ((DetLen = memoryStream.Read(DetectBuff, 0, DetectBuff.Length)) > 0 && !Det.IsDone())
                    {
                        Det.HandleData(DetectBuff, 0, DetectBuff.Length);
                    }
                    Det.DataEnd();
                    //得到字符集合
                    if (Det.GetDetectedCharset() != null)
                    {
                        if (encoding == "")
                        {
                            //得到字符集
                            encoding = Det.GetDetectedCharset();
                        }
                    }
                    //设置流指向头
                    memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
                }

                #endregion
                System.Text.Encoding   myencoding     = System.Text.Encoding.GetEncoding(encoding);
                System.IO.StreamReader mystreamreader = new System.IO.StreamReader(memoryStream, myencoding);
                strHtmlContent = mystreamreader.ReadToEnd();
                mystreamreader.Dispose();
                if (myresponse != null)
                {
                    myresponse.Close();
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(DateTime.Now + "获取网页内容出错:url:" + url + "\r\n" + ex.Message + " " + (ex.StackTrace == null ? " " : " " + ex.StackTrace));
            }
            finally
            {
                mystream.Close();
                mystream.Dispose();
                // HttpWebRequest 不会自己销毁对象
                //销毁关闭连接
                if (myrequest != null)
                {
                    myrequest.Abort();
                }
            }
            return(strHtmlContent);
        }
Beispiel #22
0
 /// <summary>
 /// Abort this instance.
 /// </summary>
 public override void Abort()
 {
     _httpWebRequest.Abort();
 }