public dynamic CommonQuery(string service, string method, params object[] args) { var arguments = JsonConvert.SerializeObject(args); var client = new HttpClient(); var response = client.Post("http://www.gzzb.gd.cn/qyww/json/", new Dictionary<string, object> { { "service", service }, { "arguments", arguments }, { "method", method } }); //Debug.WriteLine(response); return JsonConvert.DeserializeObject<dynamic>(response); }
public string FindCompanyId(string companyName) { var client = new HttpClient(); var html = client.Post("http://www.gzzb.gd.cn/cms/wz/view/sccx/QyxxServlet?siteId=1", new Dictionary<string, object> { { "qyxx_qylx","01"}, {"qyxx_qymc",companyName} }, encoding: Encoding.GetEncoding("GBK")); /* <a href="/qyww/sccx/basicInfoview.jsp?qybh=121707" target="_blank">浙江中一建设有限公司</a> </td> */ var doc = new HtmlDocument(); doc.LoadHtml(html); //取得表格里第二个tr里有url的链接 var linkNodes = doc.DocumentNode.SelectNodes("//div[@class=\"bszn_right_table\"]/table/tr/td/a[@href]"); if (linkNodes == null || linkNodes.Count == 0) { throw new Exception(string.Format("Html找不到对应信息。 Html:{0}", html)); } //取得a的文本里包含公司名称的url var link = (from node in linkNodes where !string.IsNullOrEmpty(node.InnerText) && node.InnerText.Equals(companyName) select node.Attributes["href"]).FirstOrDefault(); if (link == null || string.IsNullOrEmpty(link.Value)) { throw new Exception(string.Format("Html找不到对应公司信息。 companyName:{0}", companyName)); } //取得url里的qybh(企业编号) var id = ParseCompanyId(link.Value); return id; }
public override void Login(string name, string password) { samwaterClient = new HttpClient(); try { var cookies = new CookieCollection(); cookies.Add(new Cookie("affiliate", "sports.samwater.com")); cookies.Add(new Cookie("last_visit", "asia")); cookies.Add(new Cookie("lang", "zh-cn")); cookies.Add(new Cookie("visited", "yes")); samwaterClient.CookieContainer.Add(new Uri("http://sports.samwater.com"), cookies); } catch (Exception ex) { throw ex; } try { int retryNum = 0; while (retryNum < 10) { var homeHtml = samwaterClient.Get("http://sports.samwater.com/hometop.aspx?ip=&p="); var match = RegexValidImagePath.Match(homeHtml); var imagePath = "http://sports.samwater.com/" + match.Groups[1].Value; var imageCode = GetImageCode(imagePath); var param = new Dictionary<string, object>{ {"id",name}, {"password",password}, {"code", imageCode}, {"page","default"}, {"lang","zh-cn"}, {"key", GetElementValue(homeHtml,"key")}, {"tea",GetElementValue(homeHtml,"tea")}, {"tzDiff","0"}, {"sv","B07"}}; var loginResponse = samwaterClient.Post("http://sports.samwater.com/processlogin.aspx", param); if (loginResponse.Contains("/welcome.aspx")) { //<form name='f' action='http://2u9336mv9j85.asia.samwater.com/welcome.aspx' method='get'> var regexFormAction = new Regex("<form [^>]+action='([^']+)'", RegexOptions.IgnoreCase | RegexOptions.Multiline); var matchFormAction = regexFormAction.Match(loginResponse); if (matchFormAction.Success) { var welcomeUrl = matchFormAction.Groups[1].Value; var uri = new Uri(welcomeUrl); this.BaseUrl = "http://" + uri.Host; var welcomeParam = MatchDictionary(RegexHiddenElement, loginResponse); var welcomeResponse = samwaterClient.GetWebResponse(welcomeUrl, welcomeParam); var home_url = welcomeResponse.ResponseUri.AbsoluteUri; welcomeResponse.Close(); ///webroot/restricted/DefaultFrame.aspx?loginname=058c1914b83364d6159ce73b5dc859f8&redirect=true&site=asiabsi var regexLoginName = new Regex("loginname=([^&]+)&"); var matchLoginName = regexLoginName.Match(home_url); if (matchLoginName.Success) { this.Session["uid"] = matchLoginName.Groups[1].Value; } Debug.WriteLine("登陆成功!" + (retryNum > 0 ? "重试次数:" + retryNum : "")); break; } } else { Debug.WriteLine(loginResponse); } retryNum++; } } catch (Exception ex) { throw ex; } if (this.Session["uid"] == null || this.Session["uid"] == "") { throw new Exception("登录失败!"); } }