public void addPic(string picUrl, int stat)
{ HttpWebRequest request = (HttpWebRequest)WebRequest.Create(picUrl);
HttpWebResponse res;
try
{
res = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
res = (HttpWebResponse)ex.Response;
} Stream stream = res.GetResponseStream();
StreamReader read = new StreamReader(stream, Encoding.Default);
string str = read.ReadToEnd();
res.Close();
stream.Dispose();
stream.Close();
read.Dispose();
read.Close();
HtmlDocument html = new HtmlDocument();
html.LoadHtml(str);
HtmlNode rootNode = html.DocumentNode;
HtmlNode root = rootNode.SelectSingleNode("//html[1]/body[1]");
string strRoot = "http://car.autohome.com.cn";
if (root != null)
{
HtmlNode temp = HtmlNode.CreateNode(root.OuterHtml);
HtmlNode end = temp.SelectSingleNode("//div[@clss='RemSerise']");
HtmlNode imageLink = temp.SelectSingleNode("//img[@id='img']");
HtmlNodeCollection daohangas = temp.SelectNodes("//div[@class='breadnav']/a");
HtmlNodeCollection yanse = temp.SelectNodes("//a[@class='red']");
if (imageLink != null && daohangas != null)
{ string aaaaa = daohangas[0].OuterHtml;
string typeName = string.Empty; ///图片所属分类
string color = string.Empty; ///图片所属颜色
string seriesName = string.Empty;
string modelsItemName = string.Empty;
seriesName = daohangas[3].InnerText;
string[] yars = daohangas[4].InnerText.Split(' ');
int year = int.Parse(yars[0].Replace("款", ""));
modelsItemName = string.Concat(seriesName, " ", daohangas[4].InnerText); if (yanse.Count == 2)
{
typeName = Regex.Replace(yanse[0].InnerText, @"[(*\d张)]", "", RegexOptions.IgnoreCase);
color = Regex.Replace(yanse[1].InnerText, @"[(*\d张)]", "", RegexOptions.IgnoreCase);
}
else if (yanse.Count == 1)
typeName = Regex.Replace(yanse[0].InnerText, @"[(*\d张)]", "", RegexOptions.IgnoreCase); if (imageLink != null && imageLink.Attributes["src"] != null)
{
string gaoqingUrl = string.Empty;
string strabc = imageLink.Attributes["src"].Value;
gaoqingUrl = strabc.Replace("v_", "");
string mime = string.Empty;
string netUrl = Regex.Match(str, @"var nexturl *= *'([^']*)' *;?").Groups[1].Value;
netUrl = string.Concat(strRoot, netUrl); #region -插入图片,车型信息,颜色信息
if (mo2 == null)
{
if (!this.ModelsItemService.IsExistsModels(seriesName, year, stat, out mo2))
{
mo2 = new Models();
si2 = this.SeriesService.GetSeriesBySeriesName(seriesName);
DateTime dt = new DateTime(year, 1, 1);
mo2.Period = dt;
mo2.Status = stat;
mo2.CreateOn = DateTime.Now;
mo2.SeriesId = si2.SeriesId;
this.ModelsItemService.InsertModels(mo2); }
} if (mi2 == null)
{
if (!this.ModelsItemService.IsExistsModelsItem(modelsItemName, out mi2))
{
mi2 = new ModelsItem();
mi2.ModelsId = mo2.ModelsId;
mi2.Config = "";
mi2.Options = "";
mi2.CreateOn = DateTime.Now;
mi2.Name = modelsItemName;
this.ModelsItemService.InsertModelsItem(mi2); }
}
#region -Colors
if (color != "")
{
if (!this.ModelsItemService.IsExistsColor(color, out mic2))
{
mic2 = new ModelsItemColor();
mic2.Key = color;
mic2.Value = "";
this.ModelsItemService.InsertModelsItemColor(mic2);
}
if (!this.ModelsItemService.IsExistsItemColorMapping(mi2.ModelsItemId
, mic2.ModelsItemColorId, null))
{
this.ModelsItemService.AddModelsItemToColor(mi2.ModelsItemId
, mic2.ModelsItemColorId, "");
}
}
else
mic2 = null;
#endregion #endregion
picUrlList.Add(new string[] { gaoqingUrl, (seriesName + modelsItemName), mic2 == null ? null : mic2.ModelsItemColorId.ToString(), typeName, mi2 == null ? null : mi2.ModelsItemId.ToString() });
if (end==null)
{
cccccccccccc++;
addPic(netUrl, stat);
}
else
{
downRemoteImg2(picUrlList);
cccccccccccc = 1;
mo2 = null;
mi2 = null;
picUrlList = new List<string[]>();
}
}
}
} //throw new Exception("一款结束");
}假死·怎么办
System.Threading.Thread