private static async void doHttp(SdSource source, String url, Dictionary<String, String> args, SdNode config, __CacheBlock cache, HttpCallback callback) { var encoding = Encoding.GetEncoding(config.encode()); AsyncHttpClient client = new AsyncHttpClient(); if (config.isInCookie() && string.IsNullOrEmpty(source.cookies()) == false) { client.Cookies(source.cookies()); } client.Header("User-Agent", source.ua()); client.Encoding(config.encode()); string newUrl = null; if (url.IndexOf(" ") >= 0) newUrl = Uri.EscapeUriString(url); else newUrl = url; if (config.isInReferer()) { client.Header("Referer", source.buildReferer(config, url)); } if (string.IsNullOrEmpty(config.accept) == false) { client.Header("Accept", config.accept); client.Header("X-Requested-With", "XMLHttpRequest"); } client.Url(newUrl); string temp = null; try { AsyncHttpResponse rsp = null; if ("post".Equals(config.method)) rsp = await client.Post(args); else rsp = await client.Get(); if (rsp.StatusCode == HttpStatusCode.Ok) { source.setCookies(rsp.Cookies); temp = rsp.GetString(); } } catch(Exception ex) { Util.log(source, "HTTP", ex.Message); } if (temp == null) { if (cache == null) callback(-2, null); else callback(1, cache.value); } else callback(1, temp); }
public static void log(SdSource source, String tag, String msg, Exception tr) { Debug.WriteLine(msg, tag); if (SdApi._listener != null) { SdApi._listener.run(source, tag, msg, tr); } }
//调用函数 public string callJs(SdSource source, string fun, params object[] args) { try { return engine.CallFunction(fun, args.ToArray()).ToString(); } catch (Exception ex) { Util.log(source, "JsEngine.callJs:" + fun, ex.Message, ex); return null; } }
internal SdJscript(SdSource source, XElement node) { s = source; if (node == null) { code = ""; require = new SdNode(source, null); } else { code = node.Element("code").Value.Trim(); require = new SdNode(source, node.Element("require")); } }
internal SdJscript(SdSource source, XElement node) { s = source; if (node == null) { code = ""; require = Util.createNode(source).buildForNode(null); } else { code = node.Element("code").Value.Trim(); require = Util.createNode(source).buildForNode(node.Element("require")); } }
public JsEngine loadJs(SdSource source, string funs) { try { engine.RunScript(funs);//预加载了批函数 } catch (Exception ex) { Util.log(source, "JsEngine.loadJs", ex.Message, ex); throw ex; } return this; }
public JsEngine(SdSource s) { source = s; engine = new ChakraHost(); //if (s != null) { // engine.RegisterFunction("print", (args) => // { // //if (args.Length > 0) { // // Util.log(source, "JsEngine.print", args[0].ConvertToString().ToString()); // //} // }); //} }
public async static void http(SdSource source, bool isUpdate, HttpMessage msg) { log(source, "Util.http", msg.url); String cacheKey2 = null; String args = ""; if (msg.form == null) { cacheKey2 = msg.url; } else { StringBuilder sb = new StringBuilder(); sb.Append(msg.url); foreach (String key in msg.form.Keys) { sb.Append(key).Append("=").Append(msg.form[key]).Append(";"); } cacheKey2 = sb.ToString(); args = cacheKey2; } String cacheKey = cacheKey2; __CacheBlock block = await cache.get(cacheKey); if (isUpdate == false && msg.config.cache > 0) { if (block != null && block.isOuttime(msg.config) == false) { log(source, "Util.incache.url", msg.url); msg.callback(1, msg, block.value, null); return; } } doHttp(source, msg, block, (code, msg2, data, url302) => { if (code == 1) { cache.save(cacheKey, data); } msg.callback(code, msg2, data, url302); }); source.DoTraceUrl(msg.url, args, msg.config); }
// //-------------------------------- // public static void log(SdSource source, SdNode node, String url, String json, int tag) { log(source, node.name, "tag=" + tag); if (url == null) { log(source, node.name, "url=null"); } else { log(source, node.name, url); } if (json == null) { log(source, node.name, "json=null"); } else { log(source, node.name, json); } }
public async static void http(SdSource source, bool isUpdate, String url, Dictionary<String, String> args, SdNode config, HttpCallback callback) { __CacheBlock block = null; if (isUpdate == false && config.cache > 0) { block = cache.get(url); } if (block != null) { if (config.cache == 1 || block.seconds() <= config.cache) { await Task.Delay(100); callback(1, block.value); return; } } doHttp(source, url, args, config, block,(code, data) => { if (code == 1 && config.cache > 0) { cache.save(url, data); } callback(code, data); }); }
public async static void http(SdSource source, bool isUpdate, HttpMessage msg) { log(source, "Util.http", msg.url); String cacheKey2 = null; String args = ""; if (msg.form == null) cacheKey2 = msg.url; else { StringBuilder sb = new StringBuilder(); sb.Append(msg.url); foreach (String key in msg.form.Keys) { sb.Append(key).Append("=").Append(msg.form[key]).Append(";"); } cacheKey2 = sb.ToString(); args = cacheKey2; } String cacheKey = cacheKey2; __CacheBlock block = await cache.get(cacheKey); if (isUpdate == false && msg.config.cache > 0) { if (block != null && block.isOuttime(msg.config) == false) { log(source, "Util.incache.url", msg.url); msg.callback(1, msg, block.value, null); return; } } doHttp(source, msg, block, (code, msg2, data, url302) => { if (code == 1) { cache.save(cacheKey, data); } msg.callback(code, msg2, data, url302); }); source.DoTraceUrl(msg.url, args, msg.config); }
public virtual SdNodeSet createNodeSet(SdSource source) { return(new SdNodeSet(source)); }
public static void log(SdSource source, String tag, String msg, Exception tr) { Debug.WriteLine(msg, tag); if (SdSource.logListener != null) { SdSource.logListener(source, tag, msg, tr); } }
private static void log(SdSource source, String tag, String msg) { Debug.WriteLine(msg, tag); if (SdSource.logListener != null) { SdSource.logListener(source, tag, msg, null); } }
// //-------------------------------- // public static void log(SdSource source, SdNode node, String url, String json) { if (url == null) log(source, node.name, "url=null"); else log(source, node.name, url); if (json == null) log(source, node.name, "json=null"); else log(source, node.name, json); }
public SdNode() { this.source = null; }
private SdNode(SdSource source) { this.source = source; }
public static SdNodeSet createNodeSet(SdSource source) { return(SdApi._factory.createNodeSet(source)); }
private static async void doHttp(SdSource source, HttpMessage msg, __CacheBlock cache, HttpCallback callback) { var encoding = Encoding.GetEncoding(msg.config.encode()); AsyncHttpClient client = new AsyncHttpClient(); client.UserAgent(msg.config.ua()); client.Encoding(msg.config.encode()); foreach (String key in msg.header.Keys) { client.Header(key, msg.header[key]); } string newUrl = null; if (msg.url.IndexOf(" ") >= 0) newUrl = Uri.EscapeUriString(msg.url); else newUrl = msg.url; client.Url(newUrl); string temp = null; AsyncHttpResponse rsp = null; try { if ("post".Equals(msg.config.method)) rsp = await client.Post(msg.form); else rsp = await client.Get(); if (rsp.StatusCode == HttpStatusCode.OK) { source.setCookies(rsp.Cookies); temp = rsp.GetString(); if (string.IsNullOrEmpty(rsp.location) == false) { Uri uri = new Uri(msg.url); rsp.location = uri.Scheme + "://" + uri.Host + rsp.location; } } } catch (Exception ex) { Util.log(source, "HTTP", ex.Message); } if (temp == null) { if (cache == null || cache.value == null) callback(-2, msg, null, rsp.location); else callback(1, msg, cache.value, rsp.location); } else callback(1, msg, temp, rsp.location); }
public static SdNodeSet createNodeSet(SdSource source) { return SdApi._factory.createNodeSet(source); }
public static void log(SdSource source, String tag, String msg) { Debug.WriteLine(msg, tag); if (SdApi._listener != null) { SdApi._listener.run(source, tag, msg, null); } }
//--------------- public SdNodeSet(SdSource source) { this.source = source; }
internal SdNodeSet(SdSource source, XElement element) :this(source){ loadByElement(element); }
public DdNode(SdSource source) : base(source) { }
private static async void doHttp(SdSource source, HttpMessage msg, __CacheBlock cache, HttpCallback callback) { var encoding = Encoding.GetEncoding(msg.config.encode()); AsyncHttpClient client = new AsyncHttpClient(); client.UserAgent(msg.config.ua()); client.Encoding(msg.config.encode()); foreach (String key in msg.header.Keys) { client.Header(key, msg.header[key]); } string newUrl = null; if (msg.url.IndexOf(" ") >= 0) { newUrl = Uri.EscapeUriString(msg.url); } else { newUrl = msg.url; } client.Url(newUrl); string temp = null; AsyncHttpResponse rsp = null; try { if ("post".Equals(msg.config.method)) { rsp = await client.Post(msg.form); } else { rsp = await client.Get(); } if (rsp.StatusCode == HttpStatusCode.OK) { source.setCookies(rsp.Cookies); temp = rsp.GetString(); if (string.IsNullOrEmpty(rsp.location) == false) { Uri uri = new Uri(msg.url); rsp.location = uri.Scheme + "://" + uri.Host + rsp.location; } } } catch (Exception ex) { Util.log(source, "HTTP", ex.Message); } if (temp == null) { if (cache == null || cache.value == null) { callback(-2, msg, null, rsp.location); } else { callback(1, msg, cache.value, rsp.location); } } else { callback(1, msg, temp, rsp.location); } }
public SdNode(SdSource source, XElement cfg) { this.source = source; _isEmpty = (cfg == null); if (cfg != null) { this.name = cfg.Name.LocalName; this.title = cfg.Attribute("title")?.Value; this.method = cfg.Attribute("method")?.Value; this.parse = cfg.Attribute("parse")?.Value; this.parseUrl = cfg.Attribute("parseUrl")?.Value; this.url = cfg.Attribute("url")?.Value; this.expr = cfg.Attribute("expr")?.Value; this._run = cfg.Attribute("run")?.Value; this._encode = cfg.Attribute("encode")?.Value; this._ua = cfg.Attribute("ua")?.Value; if (string.IsNullOrEmpty(this.method)) { this.method = "get"; } { string temp = cfg.Attribute("cache")?.Value; if (string.IsNullOrEmpty(temp) == false) { int len = temp.Length; if (len == 1) { cache = int.Parse(temp); } else if (len > 1) { cache = int.Parse(temp.Substring(0, len - 1)); string p = temp.Substring(len - 1); switch (p) { case "d": cache = cache * 24 * 60 * 60; break; case "h": cache = cache * 60 * 60; break; case "m": cache = cache * 60; break; } } } } this.accept = cfg.Attribute("accept")?.Value; this.header = cfg.Attribute("header")?.Value; if (this.header == null) this.header = ""; string w = cfg.Attribute("w")?.Value; if (string.IsNullOrEmpty(w) == false) { string h = cfg.Attribute("h")?.Value; WHp = float.Parse(w) / float.Parse(h); } this.buildRef = cfg.Attribute("buildRef")?.Value; this.buildUrl = cfg.Attribute("buildUrl")?.Value; this.buildKey = cfg.Attribute("buildKey")?.Value; this.buildWeb = cfg.Attribute("buildWeb")?.Value; this.addKey = cfg.Attribute("addKey")?.Value; string _addPage = cfg.Attribute("addPage")?.Value; if (string.IsNullOrEmpty(_addPage) == false) { this.addPage = int.Parse(_addPage); } //搜索物有 this.args = cfg.Attribute("args")?.Value; if (cfg.HasElements) { _items = new List<SdNode>(); _adds = new List<SdNode>(); foreach (XElement e1 in cfg.Elements()) { if (e1.Name.LocalName.Equals("item")) { SdNode temp = new SdNode(source).buildForItem(e1, this); _items.Add(temp); } else { SdNode temp = new SdNode(source).buildForAdd(e1, this); _adds.Add(temp); } } } } }
internal SdNodeSet(SdSource source) { _items = new Dictionary<string, ISdNode>(); _source = source; }
public virtual SdNode createNode(SdSource source) { return new SdNode(source); }
public virtual SdNodeSet createNodeSet(SdSource source) { return new SdNodeSet(source); }
private string _ua; //http ua #endregion Fields #region Constructors public SdNode(SdSource source) { this.source = source; }
public override SdNodeSet createNodeSet(SdSource source) { return new DdNodeSet(source); }
public virtual SdNode createNode(SdSource source) { return(new SdNode(source)); }