Ответы пользователя по тегу C#
  • Программа на C# + VK API?

    @continue Автор вопроса
    Нужно зациклить выполнение программы
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Net;
    using System.IO;
    using System.Web;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;
    
    namespace ConsoleApplication14
    {
        class Program
        {
            private static System.Timers.Timer aTimer;
            public static void Main()
            {
                /*string link_in_pogoda = "http://export.yandex.ru/weather-ng/forecasts/29642.xml";
                string resultPage_pogoda = "";
                HttpWebRequest request_pogoda = (HttpWebRequest)WebRequest.Create(link_in_pogoda);
                HttpWebResponse response_pogoda = (HttpWebResponse)request_pogoda.GetResponse();
                Encoding Code_pogoda = Encoding.GetEncoding(1251);
                using (StreamReader sr_pogoda = new StreamReader(response_pogoda.GetResponseStream(), Encoding.UTF8, true))
                {
                    resultPage_pogoda = sr_pogoda.ReadToEnd();
                    sr_pogoda.Close();
                }
                Console.WriteLine(resultPage_pogoda); */
                while (true)
                {
                    aTimer = new System.Timers.Timer();
                    aTimer.Interval = 600;
                    aTimer = new System.Timers.Timer(2000);
                    aTimer.Enabled = true;
                    aTimer.Elapsed += OnTimedEvent;
                    Console.ReadLine();
                }
            }
            private static void OnTimedEvent(Object source, System.Timers.ElapsedEventArgs e)
            {
    
                string link = protocol + url + method + count + ampersand + token + ampersand + expires_in + ampersand + user_id + ampersand + revoke;
                string resultPage = "";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(link);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                Encoding Code = Encoding.GetEncoding(1251);
                using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8, true))
                {
                    resultPage = sr.ReadToEnd();
                    sr.Close();
                }
                JObject testObject = JObject.Parse(resultPage);
                string body = "";
                int id_vk = -1;
                foreach (JToken j in testObject["response"])
                {
                    if (j is JObject)
                    {
                        body = (string)j["body"];
                        id_vk = (int)j["uid"];
                    }
                }
                if (body.Contains("погод"))
                {
                    const string protocol_send_messenge = "https://";
                    const string url_send_messenge = "api.vkontakte.ru/method/";
                    string user_id_ = "user_id=" + id_vk;
                    const string method_send_messenge = "messages.send?";
                    const string messenge = "message=тестинг";
                    string link_send_messenge = protocol_send_messenge + url_send_messenge + method_send_messenge;
                    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(link_send_messenge);
                    req.Method = "POST";
                    req.ContentType = "application/x-www-form-urlencoded";
    
                    byte[] SomeBytes = null;
                    string FormParams = user_id_ + ampersand + messenge + ampersand + token;
                    SomeBytes = Encoding.UTF8.GetBytes(FormParams);
                    req.ContentLength = SomeBytes.Length;
                    Stream newStream = req.GetRequestStream();
                    newStream.Write(SomeBytes, 0, SomeBytes.Length);
                }
            }
        }
    }
    Ответ написан
    3 комментария
  • "System.InvalidOperationException" в Newtonsoft.Json.dll - C#?

    @continue Автор вопроса
    Решение, данной проблемы:
    string body = "";
    foreach (JToken j in testObject["response"])
    {
        if (j is JObject)
        {
            body = (string)j["body"];
        }
    }
    Ответ написан
    Комментировать