问题描述
未能找到文件“d:我的文档VisualStudio2010ProjectsWindowsFormsApplication7WindowsFormsApplication7binDebughtmlPUBLIC”。代码如下:usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.IO;usingSystem.Net;usingSystem.Text.RegularExpressions;usingSystem.Windows.Forms;namespaceWindowsFormsApplication7{publicpartialclassForm1:Form{publicForm1(){InitializeComponent();this.urlText.Text="http://vip.stock.finance.sina.com.cn/quotes_service/view/vMS_tradehistory.php?symbol=sz000725&date=2012-04-25";}publicstringGetPage(stringstrURL,stringstrReferer,stringcode){returnGetPage(strURL,strReferer,code,String.Empty);}publicstringGetPage(stringstrURL,stringstrReferer,stringcode,stringcontentType){stringstrResult="";HttpWebRequesthttpWebRequest=(HttpWebRequest)WebRequest.Create(strURL);httpWebRequest.AllowAutoRedirect=true;httpWebRequest.KeepAlive=false;httpWebRequest.Accept="*/*";httpWebRequest.Referer=strReferer;httpWebRequest.Headers.Add("Accept-Encoding","gzip,deflate");httpWebRequest.UserAgent="Mozilla/4.0(compatible;MSIE6.0;WindowsNT5.1;SV1;Maxthon;.NETCLR2.0.50727)";if(string.IsNullOrEmpty(contentType)){httpWebRequest.ContentType="application/x-www-form-urlencoded";}else{httpWebRequest.ContentType=contentType;}httpWebRequest.Method="GET";if(httpWebRequest.CookieContainer==null){httpWebRequest.CookieContainer=newCookieContainer();}HttpWebResponseresponse=null;System.IO.StreamReadersr=null;response=(HttpWebResponse)httpWebRequest.GetResponse();StreamstreamReceive;stringgzip=response.ContentEncoding;if(string.IsNullOrEmpty(gzip)||gzip.ToLower()!="gzip"){streamReceive=response.GetResponseStream();}else{streamReceive=newSystem.IO.Compression.GZipStream(response.GetResponseStream(),System.IO.Compression.CompressionMode.Decompress);}sr=newSystem.IO.StreamReader(streamReceive,Encoding.GetEncoding(code));if(response.ContentLength>1){strResult=sr.ReadToEnd();}else{char[]buffer=newchar[256];intcount=0;StringBuildersb=newStringBuilder();while((count=sr.Read(buffer,0,buffer.Length))>0){sb.Append(newstring(buffer));}strResult=sb.ToString();}sr.Close();response.Close();returnstrResult;}protectedvoidGetPath()//获取存储文件路径{FolderBrowserDialogfilepath=newFolderBrowserDialog();if(filepath.ShowDialog()==DialogResult.OK){this.fileSave.Text=filepath.SelectedPath+@"";}}privatevoidbutton1_Click(objectsender,EventArgse){if(this.fileSave.Text==null||this.fileSave.Text.Equals("")){MessageBox.Show("请选择存储路径");return;}stringstr=GetPage(this.urlText.Text,"","gb2312");MatchCollectionmatches=Regex.Matches(str,"<a.*?/a>");MessageBox.Show(matches.ToString());foreach(Matchmtinmatches){if(mt.Groups[0].ToString().EndsWith("明细下载</a>")){str=mt.Groups[0].ToString();}}str=str.Substring(9);//MessageBox.Show(str);str=str.Substring(0,str.IndexOf("""));//MessageBox.Show(str);stringdt=System.DateTime.Now.ToString();dt=Regex.Replace(dt,"","-");dt=Regex.Replace(dt,"/","-");dt=Regex.Replace(dt,":","-");File.WriteAllBytes(this.fileSave.Text+dt+".xls",newWebClient().DownloadData(str));//stringmaindata=Encoding.UTF8.GetString(newWebClient().DownloadData(string.Format(str)));//MessageBox.Show(maindata);}privatevoidbutton2_Click(objectsender,EventArgse){GetPath();}privatevoidForm1_Load(objectsender,EventArgse){}}}
解决方案
解决方案二:
错误提示不就是指定文件没有检索到吗?
解决方案三:
哥们仔细看下你的正则有问题
解决方案四:
"明细下载"根本就不存在你所获取的源码里,这个是靠js输出的,我也没细看它是怎么获取的有可能是ajax,你可以自己抓包分析下