C# HttpWebRequest请求远程地址获取返回消息

HttpWebRequest请求远程地址获取返回消息

        /// <summary>
        /// 请求远程Api获取响应返回字符串
        /// </summary>
        /// <param name="apiUrl">Api地址</param>
        /// <param name="parameters">传递参数键值对</param>
        /// <param name="contentType">内容类型默认application/x-www-form-urlencoded</param>
        /// <param name="methord">请求方式默认POST</param>
        /// <param name="timeout">超时时间默认300000</param>
        /// <returns>响应字符串</returns>
        static public object GetHttpWebResponseReturnString(string apiUrl, Dictionary<string, object> parameters, string contentType = "application/x-www-form-urlencoded", string methord = "POST", int timeout = 300000)
        {
            string result = string.Empty;
            string responseText = string.Empty;
            try
            {
                if (string.IsNullOrEmpty(apiUrl))
                {
                    return DNTRequest.GetResultJson(false, "请求apiURl为空", null);
                }

                StringBuilder postData = new StringBuilder();
                if (parameters != null && parameters.Count > 0)
                {
                    foreach (var p in parameters)
                    {
                        if (postData.Length == 0)
                        {
                            postData.AppendFormat("{0}={1}", p.Key, p.Value);
                        }
                        else
                        {
                            postData.AppendFormat("&{0}={1}", p.Key, p.Value);
                        }
                    }
                }

                ServicePointManager.DefaultConnectionLimit = int.MaxValue;

                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(apiUrl);
                myRequest.Proxy = null;
                myRequest.Timeout = timeout;
                myRequest.ServicePoint.MaxIdleTime = 1000;
                if (!string.IsNullOrEmpty(contentType))
                {
                    myRequest.ContentType = contentType;
                }
                myRequest.ServicePoint.Expect100Continue = false;
                myRequest.Method = methord;
                byte[] postByte = Encoding.UTF8.GetBytes(postData.ToString());
                myRequest.ContentLength = postData.Length;

                using (Stream writer = myRequest.GetRequestStream())
                {
                    writer.Write(postByte, 0, postData.Length);
                }

                using (HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8))
                    {
                        responseText = reader.ReadToEnd();
                    }
                }
                if (!string.IsNullOrEmpty(responseText))
                {
                    result = responseText;
                }
                else
                {
                    result = "远程服务无响应,请稍后再试";
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                result = "请求异常,请稍后再试";
            }
            return result;
        }
View Code

参考

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值