C# POST、GET、包含流的POST请求

  public bool WebHttpPost(string postUrl, Stream postData, string contentType = "application/x-www-form-urlencoded")
        {
            int start = Environment.TickCount;
            int stop;
            try
            {
                string result = null;
                HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(postUrl);
                httpWebRequest.Method = "POST";
                httpWebRequest.Timeout = 1000 * 60;
                httpWebRequest.ContentType = contentType;
                httpWebRequest.MaximumResponseHeadersLength = 20971520;
                using (Stream stream = httpWebRequest.GetRequestStream())
                    postData.CopyTo(stream);
                HttpWebResponse httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
                using (Stream responseStream = httpWebResponse.GetResponseStream())
                {
                    StreamReader streamReader = new StreamReader(responseStream, Encoding.GetEncoding("UTF-8"));
                    JObject jobj = (JObject)JsonConvert.DeserializeObject(streamReader.ReadToEnd());
                    if (jobj["Result"] != null)
                        result = jobj["Result"].ToString();
                    streamReader.Close();
                }
                if (result == "True")
                {
                    stop = Environment.TickCount;
                    int time1 = stop - start;
                    Console.WriteLine(string.Format("请求所消耗的时间:{0} (ms)", time1));
                    m_log.Debug(string.Format("请求所消耗的时间:{0} (ms)", time1));
                    return true;
                }
                else
                {
                    stop = Environment.TickCount;
                    int time1 = stop - start;
                    Console.WriteLine(string.Format("请求所消耗的时间:{0} (ms)", time1));
                    m_log.Debug(string.Format("请求所消耗的时间:{0} (ms)", time1));
                    return false;
                }
            }
            catch (Exception ex)
            {
                stop = Environment.TickCount;
                int time1 = stop - start;
                Console.WriteLine(string.Format("请求所消耗的时间:{0} (ms)", time1));
                m_log.Debug(string.Format("请求所消耗的时间:{0} (ms)", time1));
                m_log.Error(ex.Message);
                return false;
            }
        }
        /// <summary>
        /// 请求是否发送日志
        /// </summary>
        /// <param name="getUrl"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        public bool WebHttpGet(string getUrl , string contentType = "text/json")
        {
            //string ret = null;
            Uri uri = new Uri(getUrl);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "GET";
            request.ContentType = contentType;//"application / x - www - form - urlencoded";// ;
            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    JObject result = (JObject)JsonConvert.DeserializeObject(reader.ReadToEnd());
                    if (result["result"].ToString() == "0")
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                    //ret = reader.ReadToEnd();
                }
            }
            catch (WebException webex)
            {
                using (var stream = webex.Response.GetResponseStream())
                using (var reader = new StreamReader(stream))
                {
                    string message = reader.ReadToEnd();
                    Exception ex = new Exception(message);
                    m_log.Error(ex.Message);
                    return false;
                }
            }
            catch (Exception ex)
            {
                m_log.Error(ex.Message);
                return false;
            }
            return true;
        }
        /// <summary>
        /// 提交日志
        /// </summary>
        /// <param name="postUrl"></param>
        /// <param name="parameter"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        public bool WebHttpPostCommon(string postUrl,string parameter, string contentType = "text/json")
        {
            string ret = null;
            Uri uri = new Uri(postUrl);
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
            request.Method = "POST";
            request.ContentType = contentType;//"application / x - www - form - urlencoded";// ;
            byte[] byteData = Encoding.UTF8.GetBytes(parameter.ToString());
            request.ContentLength = byteData.Length;
            using (Stream postStream = request.GetRequestStream())
            {
                postStream.Write(byteData, 0, byteData.Length);
            }
            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    StreamReader reader = new StreamReader(response.GetResponseStream());
                    ret = reader.ReadToEnd();
                }
            }
            catch (WebException webex)
            {
                using (var stream = webex.Response.GetResponseStream())
                using (var reader = new StreamReader(stream))
                {
                    string message = reader.ReadToEnd();
                    Exception ex = new Exception(message);
                    m_log.Error(ex.Message);
                    return false;
                }
            }
            catch (Exception ex)
            {
                m_log.Error(ex.Message);
                return false;
            }
            return true;
        }
    

  

上一篇:oracle中时间的操作函数整理


下一篇:time、datetime 时间模块