zoukankan      html  css  js  c++  java
  • Post提交

    以下两种Post提交方法都可行

    /// <summary>
            /// post 数据
            /// </summary>
            /// <param name="url"></param>
            /// <param name="Data"></param>
            /// <param name="result"></param>
            /// <returns></returns>
            public static bool PostData(string url, string Data, ref string result)
            {
                byte[] byteArray = Encoding.UTF8.GetBytes(Data);
    
                HttpWebRequest objWebRequest = (HttpWebRequest)WebRequest.Create(url);
                objWebRequest.Method = "POST";
                objWebRequest.ContentType = "application/x-www-form-urlencoded";
                objWebRequest.ContentLength = byteArray.Length;
                Stream newStream = objWebRequest.GetRequestStream();
                // Send the data. 
                newStream.Write(byteArray, 0, byteArray.Length); //写入参数 
                newStream.Close();
                HttpWebResponse response = (HttpWebResponse)objWebRequest.GetResponse();
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                try
                {
                    result = sr.ReadToEnd();// 返回的数据
                    return true;
                }
                catch (Exception ex)
                {
                    //TODO书写日志
                    Log.Fatal("[" + DateTime.Now + "]订单数据Post至通关服务接口出现异常:" + ex.Message + "
    ");
                    return false;
                }
            } 
    View Code
            /// <summary>
            /// post 数据
            /// </summary>
            /// <param name="url"></param>
            /// <param name="Data"></param>
            /// <param name="result"></param>
            /// <returns></returns>
            public static bool PostData(string url, string Data, ref string result)
            {
                System.Net.ServicePointManager.Expect100Continue = false;
                //string strsubmit = "YES";  
                ASCIIEncoding encoding = new ASCIIEncoding();
                byte[] data = encoding.GetBytes(Data);
    
                // Prepare web request...  
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
    
                myRequest.Method = "POST";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                //  myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";  
                myRequest.ContentLength = data.Length;
                Stream newStream = null;
                newStream = myRequest.GetRequestStream();
                newStream.Write(data, 0, data.Length);
                newStream.Close();
    
                try
                {
                    HttpWebResponse httpWebResponse = null;
                    httpWebResponse = (HttpWebResponse)myRequest.GetResponse();
                    StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream());
                    result = sr.ReadToEnd();// 返回的数据
                }
                catch
                {
    
                }
                return true; 
            } 
    View Code
  • 相关阅读:
    线程同步总结
    Map,HashMap,LinkedHashMap,TreeMap比较和理解
    实现自定义注解
    SSM框架中写sql在dao文件中以注解的方式
    SSM框架中写sql在xml文件中
    自由创蚁-青少年积木式编程平台正式发布了!
    回调函数
    全面理解Javascript闭包和闭包的几种写法及用途
    hover伪类
    添加背景音乐
  • 原文地址:https://www.cnblogs.com/shuai-bySty/p/5145274.html
Copyright © 2011-2022 走看看