asp.net 调用post方法并获取返回值

/// <summary>
        /// http协议 post数据 接受返回结果
        /// </summary>
        /// <param name="url"></param>
        /// <param name="paramName"></param>
        /// <param name="paramValue"></param>
        /// <returns></returns>
        protected string PostRequest(string url, List<string> paramName, List<string> paramValue)
        {
            string result = string.Empty;
            try
            {
                if (paramName != null && paramValue != null)
                {
                    // 编辑并Encoding提交的数据
                    StringBuilder sbuilder = new StringBuilder(paramName[0] + "=" + paramValue[0]);
                    for (int i = 1; i < paramName.Count; i++)
                        sbuilder.Append("&" + paramName[i] + "=" + paramValue[i]);
                    //byte[] data = Encoding.Default.GetBytes(sbuilder.ToString());
                    byte[] data = Encoding.UTF8.GetBytes(sbuilder.ToString());
                    // 发送请求
                    System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = data.Length;
                    using (Stream stream = request.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                        stream.Close();
                    }
                    // 获得回复
                    System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                    {
                        result = reader.ReadToEnd();
                        reader.Close();
                        response.Close();
                    }
                }
              
            }
            catch (Exception ex)
            {
                Log.Fatal("异常!"+ex.Message);//记录错误log
                if (ex == null)
                    ex = null;
            }
            return result;
        }

asp.net 调用post方法并获取返回值,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。