Exemplo n.º 1
0
        //Get方法:
        private string GetData(string url, string param)
        {
            string result = string.Empty;
            string status = string.Empty;

            try
            {
                HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url + param);

                httpRequest.Timeout = 10000;
                httpRequest.Method  = "GET";

                HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
                StreamReader    sr           = new StreamReader(httpResponse.GetResponseStream(), System.Text.Encoding.UTF8);

                result = sr.ReadToEnd();

                var tokenConvert = new DefaultJsonConverter();

                var token = tokenConvert.Deserialize <AccessToken>(result);

                sr.Close();
            }
            catch
            {
                Console.WriteLine(DateTime.Now + "   HTTP Get: " + status);
            }

            Console.WriteLine(status);

            return(status);
        }
Exemplo n.º 2
0
        //Post方法:
        private string Post(string url, string param, string json)
        {
            string result = string.Empty;
            string status = "-1";

            try
            {
                ASCIIEncoding encoding = new ASCIIEncoding();

                byte[] data = encoding.GetBytes(json);

                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url + param);

                //myRequest.ContentType = "application/x-www-form-urlencoded";

                myRequest.Method        = "POST";
                myRequest.ContentLength = data.Length;

                using (Stream newStream = myRequest.GetRequestStream())
                {
                    newStream.Write(data, 0, data.Length);
                    newStream.Close();
                }

                HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader    reader     = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);

                result = reader.ReadToEnd();

                var tokenConvert = new DefaultJsonConverter();

                var token = tokenConvert.Deserialize <AccessToken>(result);

                reader.Close();
            }
            catch
            {
                Console.WriteLine(DateTime.Now + "   HTTP Post: " + status);
            }

            Console.WriteLine(status);

            return(status);
        }