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
  • 相关阅读:
    java中 this和super的差别
    Servlet对文件的读写操作
    Android通过反射打造能够存储不论什么对象的万能SharedPreferences
    Solr5.3.1 SolrJ查询索引结果
    spring mvc form表单提交乱码
    多表利用DIH批量导入数据并建立索引注意事项
    【转】Solr从数据库导入数据(DIH)
    【转】solr+ajax智能拼音详解---solr跨域请求
    跨域请求获取Solr json检索结果并高亮显示
    Solr5.3.1通过copyField设置多个field(字段)同时检索
  • 原文地址:https://www.cnblogs.com/shuai-bySty/p/5145274.html
Copyright © 2011-2022 走看看