我总是用PHP完成我的Web服务。现在我正在尝试一个项目的ASP.NET,我发现自己处于一个棘手的情况。我有以下C#代码,表现为“客户端”
public void sendRequest(string URL, string JSON)
{
ASCIIEncoding Encode = new ASCIIEncoding();
byte[] data = Encode.GetBytes(JSON);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "POST";
request.ContentLength = data.Length;
request.ContentType = "application/x-www-form-urlencoded";
request.CookieContainer = cookieContainer;
Stream dataStream = request.GetRequestStream();
dataStream.Write(data, 0, data.Length);
dataStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
WebHeaderCollection header = response.Headers;
var encoding = ASCIIEncoding.ASCII;
string responseText;
using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
{
responseText = reader.ReadToEnd();
}
returnRequestTxtBx.Text = responseText;
}
好吧,现在我想处理ASPX.CS方面...... 我的问题是......如何访问我作为POST发送的数据? 在“Page_Load”方法中是否有一种方法可以处理我发送的JSON?
答案 0 :(得分:1)
要阅读服务器端的发布方法数据,请阅读HttpContext.Request.Form
方法:
protected void Page_Load(object sender, EventArgs e)
{
string value=Request.Form["keyName"];
}
或者如果您想访问行体数据,请阅读:Request.InputStream
。
如果你想要处理Json格式,请考虑Newtonsoft.Json packege。