首页 > 代码库 > C# post Json数据

C# post Json数据

原文 http://stackoverflow.com/questions/9145667/how-to-post-json-to-the-server

 

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://url");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";

using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
  //Json写入时机
string json = "{\"user\":\"test\"," + "\"password\":\"bla\"}"; streamWriter.Write(json); streamWriter.Flush(); streamWriter.Close(); } var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); }

 

C# post Json数据