C# 後臺傳送http post請求
/// <summary> /// 後臺傳送post請求 /// </summary> /// <param name="url">請求地址</param> /// <param name="param">傳送引數</param> /// <returns></returns> public static string PostHttp(string url, string param) { string strURL = url; System.Net.HttpWebRequest request; request = (HttpWebRequest)WebRequest.Create(strURL); request.Method = "POST"; request.ContentType = "application/json;charset=UTF-8"; string paraUrlCoded = param; byte[] payload; payload = System.Text.Encoding.UTF8.GetBytes(paraUrlCoded); request.ContentLength = payload.Length; Stream writer = request.GetRequestStream(); writer.Write(payload, 0, payload.Length); writer.Close(); System.Net.HttpWebResponse response; request.Timeout = 5000; response = (System.Net.HttpWebResponse)request.GetResponse(); //if (response.StatusCode == HttpStatusCode.OK) //{
//} System.IO.Stream s; s = response.GetResponseStream(); string StrDate = ""; string strValue = ""; StreamReader Reader = new StreamReader(s, Encoding.UTF8); while ((StrDate = Reader.ReadLine()) != null) { strValue += StrDate + "\r\n"; } return strValue; }