使用js發請求時,一般使用表單.json物件或者字串

$.post(url,jsonStr)

服務端獲取引數

Request.QueryString.Get();// GET引數

Request.Form.Get();// POST引數

由於一直是使用JS發請求,未注意過服務端收不到引數的情況

使用C#的HttpWebRequest發http請求時,卻發現服務端收不到引數.(出現在使用POST方式時)

於是使用最這個辦法,讀取InputStream,可以拿到引數

byte[] byts = new byte[this.Request.InputStream.Length];
Request.InputStream.Read(byts, 0, byts.Length);
json = System.Text.Encoding.Default.GetString(byts);

如果要讓服務端拿到POST的表單引數 Request.Form.Get(),那麼傳引數時如下

string postPara="id=1&name=xx";// 和 url上引數形式一樣

byte[] data = System.Text.Encoding.UTF8.GetBytes(postPara);

request.ContentLength = data.Length;

reqStream = request.GetRequestStream();
reqStream.Write(data, 0, data.Length);
reqStream.Close();