示例#1
0
        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);
            }
        }