Get,Post请求 (设置超时时间)

using Platform.Framework.NLog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Platform.Framework.Net
{
    public class WebClientManagement
    {
        /// 
        /// POST 
        /// 
        /// ?前的字符串
        /// 是要传递的参数,格式"param1=1param2=2"
        /// 
        public static string Post(string url,string postParam, int timeOut = 0)
        {
            String responseStr = null;
            try
            {
                WebDownload client = new WebDownload();
                if (timeOut != 0) client.Timeout = timeOut;
                client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");             
                byte[] postData = Encoding.UTF8.GetBytes(postParam);
                byte[] responseData = client.UploadData(url, "POST", postData);
                responseStr = Encoding.UTF8.GetString(responseData);

            }
            catch (Exception e)
            {
                responseStr = "ERR:" + e.Message;
                Log.error("ERR: " + e.Message + " URL: " + url + " PostData:" + postParam);
                return "";
            }

            return responseStr;
        }


        /// 
        /// Get 
        /// 
        /// URI
        /// 
        public static string Get(string url, int timeOut = 0)
        {
            String responseStr = null;
            try
            {
                WebDownload client = new WebDownload();
                if (timeOut != 0) client.Timeout = timeOut;
                client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                byte[] responseData = client.DownloadData(url);
                responseStr = Encoding.UTF8.GetString(responseData);

            }
            catch (Exception e)
            {
                responseStr = "ERR:" + e.Message;
                Log.error("ERR: " + e.Message + " URL: " + url);
                return "";
            }
            Log.info("get请求执行完毕,responseStr=" + responseStr);
            return responseStr;
        }

        public static string PostXmlOrJson(string url, string strPost, string contentType, int timeOut = 0)
        {
            //请求URL
            string strRet = string.Empty;
            Stream requestStream = null;
            HttpWebRequest request = null;
            try
            {
                request = (HttpWebRequest)HttpWebRequest.Create(url);
                if (timeOut != 0)
                {
                    request.Timeout = timeOut;
                }
                request.ServicePoint.Expect100Continue = false;
            }
            catch(Exception e)
            {
                strRet = "ERR:" + e.Message ;
                Log.error("ERR: " + e.Message + " URL: " + url + " PostData:" + strPost);
                return "";
            }
            request.Method = "POST";

            switch (contentType.ToUpper())
            {
                case "XML":
                    request.ContentType = "text/xml";
                    break;
                case "JSON":
                    request.ContentType = "application/json";
                    break;
                default:
                    request.ContentType = "application/x-www-form-urlencoded";
                    break;
            }

            byte[] postBytes = Encoding.UTF8.GetBytes(strPost);

            request.ContentLength = postBytes.Length;
            try
            {
                requestStream = request.GetRequestStream();
                requestStream.Write(postBytes, 0, postBytes.Length);
            }
            catch (Exception e)
            {
                Log.error("REQUESTERR: " + e.Message + " URL: " + url + " PostData:" + strPost);
                return "ERR:" + e.Message;               
            }
            finally
            {
                if (requestStream != null)
                {
                    requestStream.Flush();
                    requestStream.Close();
                }
            }
            try
            {
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader streamReader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
                    {
                        strRet = streamReader.ReadToEnd();
                    }
                }
            }
            catch (Exception ex)
            {
                Log.error("RESPONSEERR: " + ex.Message + " URL: " + url + " PostData:" + strPost);
                return "RESPONSEERR:" + ex.Message;
            }
            Log.info("post请求执行完毕,strRet=" + strRet);
            return strRet;
        }

    }

    public class WebDownload : WebClient
    {
        private int _timeout;
        public int Timeout
        {
            get
            {
                return _timeout;
            }
            set
            {
                _timeout = value;
            }
        }

        public WebDownload()
        {
            //设置时间
            this._timeout = 60000;
        }
        public WebDownload(int timeout)
        {
            this._timeout = timeout;
        }
        protected override WebRequest GetWebRequest(Uri address)
        {
            var result = base.GetWebRequest(address);
            result.Timeout = this._timeout;
            return result;
        }

    }
}

你可能感兴趣的:(Get,Post请求 (设置超时时间))