如何在.net中模拟post方式提交json数据
1个回答
展开全部
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
if( postData != null ) {
//postData为传入的数据(格式:key1=value1&key2=value2)
//这里你需要把json格式的数据转换为格式样的数据(前提是你不需要返回json格式的数据)
byte[] buffer = encoding.GetBytes(postData);
Stream stream = request.GetRequestStream();
stream.Write(buffer, 0, buffer.Length);
stream.Close();
}
//获取相应的值
using( WebResponse response = request.GetResponse() ) {
using( StreamReader reader = new StreamReader(response.GetResponseStream(), encoding) ) {
//读取相应的值
reader.ReadToEnd();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询