Exemplo n.º 1
0
        public ReturnT PerformPost <PostT, ReturnT>(string url, PostT postData, Dictionary <string, string> files)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);

            Dictionary <string, string> dicPost;

            if (typeof(PostT) == typeof(Dictionary <string, string>))
            {
                dicPost = postData as Dictionary <string, string>;
            }
            else
            {
                dicPost = HttpTools.ObjectToNameValueCollection <PostT>(postData);
            }

            List <UploadFile> postFiles = new List <UploadFile>();

            foreach (var fKey in files.Keys)
            {
                FileStream fs = File.OpenRead(files[fKey]);
                postFiles.Add(new UploadFile(fs, fKey, files[fKey], "application/octet-stream"));
            }

            //convert to nameValue
            var nvcPost = new NameValueCollection();

            foreach (KeyValuePair <string, string> pair in dicPost)
            {
                nvcPost[pair.Key] = pair.Value;
            }

            var response = HttpUploadHelper.Upload(req, postFiles.ToArray(), nvcPost);

            using (Stream s = response.GetResponseStream())
                using (StreamReader sr = new StreamReader(s))
                {
                    var responseJson = sr.ReadToEnd();
                    if (typeof(ReturnT) == typeof(string))
                    {
                        return((ReturnT)Convert.ChangeType(responseJson, typeof(ReturnT)));
                    }

                    return(fastJSON.JSON.Instance.ToObject <ReturnT>(responseJson));
                }
        }