Пример #1
0
        /// <summary>
        /// 向远程Url Post数据
        /// </summary>
        /// <param name="uri">请求URL地址</param>
        /// <param name="data">请求参数</param>
        /// <param name="serializationType">参数序列化方式</param>
        /// <returns>响应字符串</returns>
        public static string HttpPost(string uri, object data, SerializationType serializationType)
        {
            HttpWebRequest _request = HttpWebRequest.Create(uri) as HttpWebRequest;

            _request.Method      = "POST";
            _request.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
            string _requestParamter = string.Empty;

            if (data is string)
            {
                _requestParamter = (string)data;
            }
            else
            {
                if (serializationType == SerializationType.Xml)
                {
                    _requestParamter = SerializeHelper.XmlSerialize(data);
                }
                else if (serializationType == SerializationType.Json)
                {
                    _requestParamter = SerializeHelper.JsonSerialize(data);
                }
            }

            CNNWebClient _webClient = new CNNWebClient();

            _webClient.Timeout = 300;
            byte[] _responeBuffer = _webClient.UploadData(uri, "POST", Encoding.UTF8.GetBytes(_requestParamter));
            return(Encoding.UTF8.GetString(_responeBuffer));
        }
Пример #2
0
        public static string HttpPost(string uri, object data, SerializationType serializationType)
        {
            HttpWebRequest request = HttpWebRequest.Create(uri) as HttpWebRequest;

            request.Method      = "POST";
            request.ContentType = "application/x-www-form-urlencoded;charset=utf-8";

            string dataStr = string.Empty;

            if (data is string)
            {
                dataStr = (string)data;
            }
            else
            {
                if (serializationType == SerializationType.Xml)
                {
                    dataStr = SerializationHelper.XmlSerialize(data);
                    object o = SerializationHelper.XmlDeserialize(data.GetType(), dataStr);
                }
                else if (serializationType == SerializationType.Json)
                {
                    dataStr = SerializationHelper.JsonSerialize(data);
                }
            }
            CNNWebClient wc = new CNNWebClient();

            wc.Timeout = 300;
            var    t     = wc.UploadData(uri, "POST", Encoding.UTF8.GetBytes(dataStr));
            string tText = Encoding.UTF8.GetString(t);

            /*
             * System.Diagnostics.Debug.WriteLine(tText);
             * byte[] buffer;
             * Stream stream;
             * if (!string.IsNullOrWhiteSpace(dataStr))
             * {
             *  buffer = Encoding.UTF8.GetBytes(dataStr);
             *  stream = request.GetRequestStream();
             *  stream.Write(buffer, 0, buffer.Length);
             * }
             * //request.d
             * HttpWebResponse response = request.GetResponse() as HttpWebResponse;
             *
             * stream = response.GetResponseStream();
             * buffer = new byte[response.ContentLength];
             * stream.Read(buffer, 0, (int)response.ContentLength);
             * //string encoding = string.IsNullOrWhiteSpace(response.ContentEncoding) ? "uft-8" : response.ContentEncoding;
             * string responseText = Encoding.UTF8.GetString(buffer);
             * System.Diagnostics.Debug.WriteLine(responseText);
             * return responseText;
             */
            return(tText);
        }
        /// <summary>
        /// 向远程Url Post数据
        /// </summary>
        /// <param name="url">url</param>
        /// <param name="postData">post数据</param>
        /// <param name="header">Header数据</param>
        /// <param name="serializationType">post数据以及返回数据序列号处理方式</param>
        /// <returns>返回数据</returns>
        public static string Post(string url, object postData, NameValueCollection header, SerializationType serializationType)
        {
            string _serializaPostDataString = SerializePostDataObject(postData, serializationType);

            using (CNNWebClient _webClient = new CNNWebClient())
            {
                _webClient.Timeout = 300;
                if (header != null)
                {
                    _webClient.Headers.Add(header);
                }

                byte[] _responseBuffer = _webClient.UploadData(url, "POST", Encoding.UTF8.GetBytes(_serializaPostDataString));

                return(_responseBuffer != null?Encoding.UTF8.GetString(_responseBuffer) : string.Empty);
            }
        }