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方法并获取返回值,布布扣,bubuko.com

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

上一篇:js获取页面属性


下一篇:Jquery图片放大镜