码迷,mamicode.com
首页 > 移动开发 > 详细

C# ContentType: "application/json" 请求方式传json参数问题

时间:2017-07-24 11:34:36      阅读:1021      评论:0      收藏:0      [点我收藏+]

标签:read   reader   string   asc   stream   new   app   bre   bsp   

处理Http请求时遇到的ContentType为application/json方式,记录下这种Post请求方式下如何传json参数:

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

using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
    string json = new JavaScriptSerializer().Serialize(new
                {
                    user = "Foo",
                    password = "Baz"
                });

    streamWriter.Write(json);
}

var response = (HttpWebResponse)request.GetResponse();
using (var streamReader = new StreamReader(response.GetResponseStream()))
{
        var result = streamReader.ReadToEnd();
}

 

C# ContentType: "application/json" 请求方式传json参数问题

标签:read   reader   string   asc   stream   new   app   bre   bsp   

原文地址:http://www.cnblogs.com/liuxiaopi/p/7227553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!