Esempio n. 1
0
        public static int GetUsernameID(string displayName)
        {
            StackExchangeAPIRequestModel stackOverflowAPIRequestModel = new StackExchangeAPIRequestModel();

            string cURL = stackOverflowAPIRequestModel.cUrlGetUsernameId + displayName + "&site=stackoverflow&filter=!23IYXAR8SntfO)zqiR8*P";

            int userId = 0, json_response_user_id = 0;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(cURL);

            request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (Stream stream = response.GetResponseStream())
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        JToken json_response_object = JObject.Parse(reader.ReadToEnd());

                        JArray userArray = (JArray)json_response_object["items"];

                        foreach (JObject user in userArray.Children <JObject>())
                        {
                            foreach (JProperty userProperties in user.Properties())
                            {
                                if (userProperties.Name == "user_id")
                                {
                                    json_response_user_id = (int)userProperties.Value;
                                }

                                if (userProperties.Name == "display_name")
                                {
                                    if ((string)userProperties.Value == displayName)
                                    {
                                        userId = json_response_user_id;
                                    }
                                }
                            }
                        }
                    }

            return(userId);
        }
Esempio n. 2
0
        public static int GetUserComments(int userId)
        {
            StackExchangeAPIRequestModel stackOverflowAPIRequestModel = new StackExchangeAPIRequestModel();

            string cURL = stackOverflowAPIRequestModel.cUrlGetComments + userId + "/comments?site=stackoverflow&filter=total";

            int comments = 0;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(cURL);

            request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
            request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                using (Stream stream = response.GetResponseStream())
                    using (StreamReader reader = new StreamReader(stream))
                    {
                        JToken userArray = JObject.Parse(reader.ReadToEnd());
                        comments = (int)userArray["total"];
                    }

            return(comments);
        }