You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

249 lines
8.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Ivony.Html;
using Ivony.Html.Parser;
using System.Net;
using System.IO;
using System.Text;
using System.Data.SqlClient;
namespace web.Classes
{
public class WHLStatusSearchTools
{
private static string _COOKIES = "";
private static string _VIEWSTATUS = "";
private static string _BLNO = "";
public List<WHLModel> GetWHLData(string blno)
{
if (blno == "")
{
return null;
}
_BLNO = blno;
//string status = GetViewStatus();
HttpHelper http = new HttpHelper();
string whlURL = "http://www.wanhai.com/views/Main.xhtml";
HttpItem item = new HttpItem()
{
URL = whlURL,//URL 必需项
Method = "get"//URL 可选项 默认为Get
};
HttpResult result1 = http.GetHtml(item);
_COOKIES = result1.Cookie;
string html = result1.Html;
IHtmlDocument document = new JumonyParser().Parse(html);
_VIEWSTATUS = document.FindFirst("input[id=javax.faces.ViewState]").Attribute("value").Value();
return GetWHLData();
}
public string GetWHLHTMLData(string blno)
{
List<WHLModel> list = GetWHLData(blno);
string html= TransferModelListToHTML(list);
return html;
}
public string GetWHLHTMLDataWithHBLNO(string hblno)
{
//获取主提单号,通过主提单号爬数据
string mblno = GetMBLNOWithHBLNO(hblno);
List<WHLModel> list = GetWHLData(mblno);
//根据分单号关联的箱号进行过滤
string[] ctnList = GetCtnnoWithHBLNO(hblno);
if (ctnList==null)
{
return "";
}
List<WHLModel> finnalList = new List<WHLModel>();
foreach (var ctn in ctnList)
{
var model = list.Find(w => w.CtnrNo.Contains(ctn));
if (model!=null)
{
finnalList.Add(model);
}
}
if (finnalList.Count==0)
{
return "";
}
string html = TransferModelListToHTML(finnalList);
return html;
}
public List<WHLModel> GetWHLData () {
string whlURL = "http://www.wanhai.com/views/quick/cargo_tracking.xhtml";
WebRequest request = WebRequest.Create(whlURL);
request.Method = "POST";
string postData = string.Format("cargoTrackListBean=argoTrackListBean&cargoType=2&q_ref_no1="+_BLNO+"&j_idt7=Query&javax.faces.ViewState="+_VIEWSTATUS);
byte[] data = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
request.Headers.Add("Cookie", _COOKIES);
request.ContentLength = data.Length;
Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string url = response.ResponseUri.AbsoluteUri;
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
return TransferWHLHTMLString(content);
}
private static List<WHLModel> TransferWHLHTMLString (string html) {
IHtmlDocument document = new JumonyParser().Parse(html);
IEnumerable<IHtmlElement> result = document.Find("table[class=tbl-list]");
IHtmlElement table = result.First();
IEnumerable<IHtmlElement> trs = table.Find("tr");
List<WHLModel> list = new List<WHLModel>();
for (int i = 0; i < trs.Count(); i++)
{
WHLModel model = new WHLModel();
if (i == 0)
{
continue;
}
IHtmlElement tr = trs.ElementAt(i);
IEnumerable<IHtmlElement> tds = tr.Find("td");
for (int n = 0; n < tds.Count(); n++)
{
if (n == 0 || n == tds.Count() - 1)
{
continue;
}
IHtmlElement td = tds.ElementAt(n);
switch (n)
{
case 1:
model.CtnrNo = td.InnerHtml();
break;
case 2:
model.CtnrISO = td.InnerHtml();
break;
case 3:
model.BLNo = td.InnerHtml();
break;
case 4:
model.StatusName = td.InnerHtml();
break;
case 5:
model.CtnrDate = td.InnerHtml();
break;
case 6:
model.CtnrDepotName = td.InnerHtml();
break;
case 7:
model.PlaceName = td.InnerHtml();
break;
case 8:
model.VoyNo = td.InnerHtml();
break;
case 9:
model.VesselName = td.InnerHtml();
break;
default:
break;
}
}
list.Add(model);
}
return list;
}
private static string TransferModelListToHTML(List<WHLModel> list) {
StringBuilder sb = new StringBuilder();
sb.Append("<table>");
sb.Append("<tr>");
sb.Append("<td>CTNR No.</td>");
sb.Append("<td>Ctnr ISO</td>");
sb.Append("<td>BL no.</td>");
sb.Append("<td>Status Name</td>");
sb.Append("<td>Ctnr Date</td>");
sb.Append("<td>Ctnr Depot Name</td>");
sb.Append("<td>Place Name</td>");
sb.Append("<td>Voy No</td>");
sb.Append("<td>Vessel Name</td>");
sb.Append("</tr>");
foreach (var item in list)
{
sb.Append("<tr>");
sb.Append("<td>" + item.CtnrNo + "</td>");
sb.Append("<td>" + item.CtnrISO + "</td>");
sb.Append("<td>" + item.BLNo + "</td>");
sb.Append("<td>" + item.StatusName + "</td>");
sb.Append("<td>" + item.CtnrDate + "</td>");
sb.Append("<td>" + item.CtnrDepotName + "</td>");
sb.Append("<td>" + item.PlaceName + "</td>");
sb.Append("<td>" + item.VoyNo + "</td>");
sb.Append("<td>" + item.VesselName + "</td>");
sb.Append("</tr>");
}
sb.Append("</table>");
return sb.ToString();
}
private string GetMBLNOWithHBLNO(string hblno)
{
string mblno = "";
string sql = "select MBLNO from MHBLNODetails where HBLNO like '%"+hblno+"%'";
List<string> mblnos = new List<string>();
using (SqlDataReader reader = SQLHelper.GetReader(sql))
{
while (reader.Read())
{
mblnos.Add(reader[0].ToString());
}
}
if (mblnos.Count==1)
{
mblno = mblnos[0];
}
return mblno;
}
private string[] GetCtnnoWithHBLNO(string HBLNO)
{
string sql = "select CTNNO from HBLNOCTNDetails where HBLNO = '"+HBLNO+"'";
var rst = SQLHelper.ExcuteScalarSQL(sql);
if (rst == DBNull.Value||rst==null)
{
return null;
}
string ctnnos = rst.ToString();
string[] arr = ctnnos.Split(',');
return arr;
}
}
public class WHLModel
{
public string CtnrNo { get; set; }
public string CtnrISO { get; set; }
public string BLNo { get; set; }
public string StatusName { get; set; }
public string CtnrDate { get; set; }
public string CtnrDepotName { get; set; }
public string PlaceName { get; set; }
public string VoyNo { get; set; }
public string VesselName { get; set; }
}
}