• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

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

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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人参与评论

请发表评论

全部评论

专题导读
上一篇:
【C++多线程】共享数据保护发布时间:2022-07-13
下一篇:
C#将List转换为Json,将DataSet转成ListT发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap