以下两种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 + "\r\n"); return false; } }
/// <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; }