Exemplo n.º 1
0
        private static string Execute(ApiQuery apiQuery)
        {
            var param = new Dictionary <string, string>(apiQuery.Param)
            {
                { "access_token", VkapiLibrary.Instance.AccessTokien },
                { "lang", "ru" },
                { "v", "5.37" }
            };


            var url      = "https://api.vk.com/method/" + apiQuery.Method;
            var postData = "";

            foreach (var val in param)
            {
                postData += val.Key + "=" + Uri.EscapeDataString(val.Value) + "&";
            }
            Log.Debug("API Request " + url + " " + postData);
            var byteArray = Encoding.UTF8.GetBytes(postData);

            // todo keep-alive
            var webReq = (HttpWebRequest)WebRequest.Create(url);

            webReq.Timeout     = Properties.Settings.Default.Timeout * 1000;
            webReq.Method      = "POST";
            webReq.ContentType = "application/x-www-form-urlencoded";
            //WebReq.Headers["Cookie"] = "remixlang=0; remixchk=5; remixsid=" + SID;

            webReq.ContentLength = byteArray.Length;
            Stream dataStream = webReq.GetRequestStream();

            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();

            var stream = webReq.GetResponse().GetResponseStream();

            if (stream == null)
            {
                Log.Error("VK API EXECUTE ERROR(response null)" + url);
                throw new Exception("VK API EXECUTE ERROR");
            }
            return(new StreamReader(stream, Encoding.UTF8).ReadToEnd());
        }
Exemplo n.º 2
0
        private void DoWork()
        {
            while (_isAlive)
            {
                Thread.Sleep(Timeout);
                IApiQuery query;
                var       qList        = new List <IApiQuery>();
                var       executeQuery = "var result = [];";
                var       i            = 0;
                while (i < 25 && _concurrentQueue.TryDequeue(out query))
                {
                    qList.Add(query);
                    executeQuery += query.ToString();
                    i++;
                }
                if (qList.Count == 0)
                {
                    _resetEvent.Set();
                    continue;
                }

                Log.Info("Query execute " + i + "/" + MaxQueryInExecute);

                executeQuery += "return result;";
                var param = new Dictionary <string, string> {
                    { "code", executeQuery }
                };
                var executeApiQuery = new ApiQuery("execute", param, VkapiLibrary.Json);

                var jsonSrc = Execute(executeApiQuery);

                //Console.WriteLine(jsonSrc);
                Log.Debug("Api multiquery: " + jsonSrc);
                var    jObject = JObject.Parse(jsonSrc);
                JToken error;
                if (jObject.TryGetValue("error", out error))
                {
                    TryThrowException(jObject.GetValue("error"));
                }
                var result = jObject.GetValue("response");
                if (result.GetType() != typeof(JArray) || ((JArray)result).Count < qList.Count)
                {
                    Log.Error("VK API EXECUTE ERROR: unknown responce :" + jObject);
                    throw new Exception("VK API EXECUTE ERROR: unknown responce :" + jObject);
                }
                i = 0;
                var errorI = 0;
                foreach (var res in (JArray)result)
                {
                    if (i < qList.Count)
                    {
                        if (res.Type == JTokenType.Boolean && !(bool)res)
                        {
                            var executeErrors = (JArray)jObject.GetValue("execute_errors");
                            qList[i].Responce = executeErrors[errorI];
                            errorI++;
                        }
                        else
                        {
                            qList[i].Responce = res;
                        }
                        i++;
                    }
                }

                _resetEvent.Set();
            }
        }