- 积分
 - 1104
 
	- 贡献
 -  
 
	- 精华
 
	- 在线时间
 -  小时
 
	- 注册时间
 - 2016-9-23
 
	- 最后登录
 - 1970-1-1
 
 
 
 
 
 
 | 
	
 
登录后查看更多精彩内容~
您需要 登录 才可以下载或查看,没有帐号?立即注册 
 
 
 
x
 
本人想用C#编写一个批量下载一些天气网站上的数值产品图片的小程序,界面如下: 
 
 
思路就是循环写出各个网站图片的url(各大网站的图片命名都是规律性很强的),然后依次下载 
使用的方法主要就是: 
WebClient webClient = new WebClient(); 
webClient.DownloadFile(t7PicUrl_full, t7PathAndName); 
 
目前,对于其他的网站,如欧格、CCTV等都可以顺利实现,唯独对于天气在线网站,下载的图片都是不超过1kb的文件,无法打开。 
但是我用C#的控制台应用程序(传说中的黑窗口)手动循环下载,就可以成功下载下来,把代码移植到窗体里就不行了,也没有报任何错。 
另外还有一点,我编写了一个判断网站上某张图片是否存在的函数,对其他网站都适用,唯独对天气在线不好使,无论网站是否有这张图片,返回都是有。 
 
请教大神:出现不超过1kb的文件是怎么回事?是否跟天气在线网站的某种安全设置有关(防止短时间频繁访问)?为什么我便的那个函数对天气在线不好使? 
判断网络文件是否存在的代码: 
private static bool RemoteFileExists(string fileUrl)//判断网络文件是否存在 
        { 
            bool result = false;//下载结果 
            WebResponse response = null; 
            try 
            { 
                WebRequest req = WebRequest.Create(fileUrl); 
                req.Timeout = 180000; 
                response = req.GetResponse(); 
                result = response == null ? false : true; 
            } 
            catch (Exception ex) 
            { 
                result = false; 
            } 
            finally 
            { 
                if (response != null) response.Close(); 
            } 
 
            return result; 
        } 
 
 
循环下载天气在线网站的代码 
private void btn_t7AllDown_Click(object sender, EventArgs e) 
        { 
            string strBeginTime = t7Year + "-" + t7Month + "-" + t7Day + " " + t7Hour + ":00:00";//t7Year、t7Month、 t7Day、t7Hour是起报时间,已在前文赋值, 此处略。 
            DateTime datBeginTime = Convert.ToDateTime(strBeginTime); 
            int SJJG = 6;//网站图片的时间间隔 
            int t7PicSum = t7Long/SJJG;//每一项的图片数目 
            //遍历所有选中的checkbox,开始下载 
            foreach (Control temp in groupBox1.Controls) 
            { 
                if (temp is CheckBox && ((CheckBox)temp).Checked == true) 
                { 
                    try//下载被选中的内容 
                    { 
                        int k = 0; 
                        string t7PicType = ((CheckBox)temp).Name;//我把图片文件的类型写到了CheckBox.Name里,如z500 z850等 
                        string t7Path = MainPath + "/天气在线@UTC" + t7Day + "日" + t7Hour + "时起报/" + ((CheckBox)temp).Tag; 
                        if (!Directory.Exists(t7Path)) 
                        { 
                            Directory.CreateDirectory(t7Path); 
                        } 
                        if(t7PicType=="z500"||t7PicType=="z850") 
                        { 
                            SJJG=12;//z500 z850都是隔12小时一张图,因此需要别更改一下 
                            t7PicSum=t7Long/SJJG; 
                        } 
                        for (int i = SJJG ; i <= t7Long; i = i + SJJG) 
                        { 
                            DateTime datSJXH = datBeginTime.AddHours(i);//时间循环 
                            string strSJXH = datSJXH.ToString("yyMMddHH"); 
                            string t7Name = t7PicType + "_" + strSJXH + "_" + t7Day + t7Hour + ".gif"; 
                            string t7PicUrl_full = t7PicUrl_front + t7Year + "/" + t7Month + "/" + t7Day + "/basis" + t7Hour + "/cncn/" + t7Name + "?"; 
                            string t7PathAndName = t7Path + "/" + t7Name; 
                            //if (RemoteFileExists(t7PicUrl_full)) 
                            //{ 
                                if (File.Exists(t7PathAndName))//判断本地文件是否已存在,避免重复下载 
                                { 
                                } 
                                else 
                                { 
                                    //Thread.Sleep(5000); 
                                    WebClient webClient = new WebClient(); 
                                    webClient.DownloadFile(t7PicUrl_full, t7PathAndName);//下载文件 
                                    //MessageBox.Show(t7PicUrl_full); 
                                } 
                                k++; 
                                foreach (Control temp0 in groupBox1.Controls) 
                                { 
                                    if (temp0 is TextBox && ((TextBox)temp0).TabIndex == temp.TabIndex) 
                                        ((TextBox)temp0).Text = k.ToString() + "/" + t7PicSum.ToString();//进度条上的数字,标识“已下张数/预计张数” 
                                    if (temp0 is ProgressBar && ((ProgressBar)temp0).TabIndex == temp.TabIndex) 
                                    { 
                                        ((ProgressBar)temp0).Minimum = 0; 
                                        ((ProgressBar)temp0).Maximum = t7PicSum;//进度条 
                                        ((ProgressBar)temp0).Value = k; 
                                    } 
                                    Application.DoEvents(); 
                                } 
                            //} 
                        } 
                    } 
                    catch (Exception ex) 
                    { 
                        MessageBox.Show("t7-" + temp.Text + ":" + ex.Message); 
                    } 
                } 
            } 
 
 |   
 
 
 
 |