예제 #1
0
        /// <summary>
        ///     上传文件。formData参数附加到url
        /// </summary>
        /// <param name="formData"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public string Upload(FormData formData, string filePath)
        {
            var url  = String.Format("{0}?{1}", Url, formData == null ? "" : formData.Format());
            var data = new WebClient().UploadFile(url, "POST", filePath);

            return(Encoding.GetString(data));
        }
예제 #2
0
        /// <summary>
        /// 发送Get请求
        /// </summary>
        /// <param name="formData"></param>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T Get <T>(FormData formData)
        {
            var request = WebRequest.Create(string.Format("{0}?{1}", Url, formData.Format()));

            request.Method = "GET";
            T ret;

            using (var rep = request.GetResponse())
            {
                ret = ReadFromResponse <T>(rep);
            }
            return(ret);
        }
예제 #3
0
        /// <summary>
        ///     以post方式提交,将响应编码为字串。
        /// </summary>
        /// <param name="body"></param>
        /// <param name="formData"></param>
        /// <returns></returns>
        public string PostString(string body, FormData formData = null)
        {
            var url = String.Format("{0}{1}", Url, formData == null ? "" : string.Format("?{0}", formData.Format()));

            return(PostString(url, body));
        }
예제 #4
0
        /// <summary>
        /// 以post方式提交,将响应编码为字串。
        /// </summary>
        /// <param name="body"></param>
        /// <param name="formData"></param>
        /// <returns></returns>
        public string PostString(string body, FormData formData)
        {
            var request = CreateRequest(string.Format("{0}?{1}", Url, formData == null ? "" : formData.Format()));

            request.Method = "POST";
            var sw = new StreamWriter(request.GetRequestStream());//注意,StreamWriter不能带编码的构造,否则会在前面写入编码标识(比utf8:ef bb bf)

            sw.Write(body);
            sw.Flush();
            sw.Close();;
            using (var rep = request.GetResponse().GetResponseStream())
            {
                var ret = ReadFromResponse(rep);
                return(ret);
            }
        }
예제 #5
0
        public string GetString(FormData formData)
        {
            var url = string.Format("{0}?{1}", Url, formData.Format());

            return(ReadFromResponse(GetStream(url)));
        }
예제 #6
0
        public T Post <T>(string body, FormData formData = null)
        {
            var request = WebRequest.Create(string.Format("{0}?{1}", Url, formData == null ? "" : formData.Format()));

            request.Method = "POST";
            var sw = new StreamWriter(request.GetRequestStream());

            sw.WriteLine(body);
            sw.Flush();
            sw.Close();
            T ret;

            using (var rep = request.GetResponse())
            {
                ret = ReadFromResponse <T>(rep);
            }
            return(ret);
        }