Пример #1
0
        public static Response SendGetRequest(string url, object data, bool allowAutoRedirect)
        {
            string paramData = string.Empty;

            HttpCommunications.GetProperties(data).ToList().ForEach(x =>
            {
                paramData += string.Format("{0}={1}&", x.Key, x.Value);
            });
            paramData = paramData.TrimEnd('&');

            url = ProcessUrl(url);

            HttpWebRequest request = paramData.Length > 0 ? (HttpWebRequest)WebRequest.Create(string.Format("{0}?{1}", url, paramData)) : (HttpWebRequest)WebRequest.Create(url);

            request.AllowAutoRedirect = allowAutoRedirect;


            return(new Response(GetResponse(request)));
        }
Пример #2
0
        public static Response SendPostRequest(string url, object data, bool allowAutoRedirect)
        {
            try
            {
                string formData = string.Empty;
                HttpCommunications.GetProperties(data).ToList().ForEach(x =>
                {
                    string key = x.Key;
                    if (x.Key == "newone")
                    {
                        // this is a workaround as new is a command in C#...
                        key = "new";
                    }
                    formData += string.Format("{0}={1}&", key, x.Value);
                });
                formData = formData.TrimEnd('&');

                url = ProcessUrl(url);

                HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
                request.Method            = "POST";
                request.AllowAutoRedirect = allowAutoRedirect;
                request.Accept            = "*/*";
                request.UserAgent         = "last.fm snarls (http://www.tlhan-ghun.de/)";
                request.ContentType       = "application/x-www-form-urlencoded";

                byte[] encodedData = new UTF8Encoding().GetBytes(formData);
                request.ContentLength = encodedData.Length;

                using (Stream newStream = request.GetRequestStream())
                {
                    newStream.Write(encodedData, 0, encodedData.Length);
                }

                return(new Response(GetResponse(request)));
            }
            catch (System.Exception e)
            {
                return(null);
            }
        }