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
    {
        /// <summary>
        /// POST 
        /// </summary>
        /// <param name="url">?前的字符串</param>
        /// <param name="postParam">是要传递的参数,格式"param1=1param2=2"</param>
        /// <returns></returns>
        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;
        }


        /// <summary>
        /// Get 
        /// </summary>
        /// <param name="url">URI</param>
        /// <returns></returns>
        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;
        }

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值