使用JSON创建POST()

时间:2016-12-01 03:22:08

标签: c# json httpwebrequest

我正在学习JSON,无法弄清楚下一步。我已经创建了一个JSON Reader并将我需要的数据添加到我的阅读器中,但是我现在如何用数据实际发出POST()请求呢?

这就是我所拥有的

private void PerformJSONPost()
{
    var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://requestb.in/12frf661");
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";

    var jsonReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(@"{ ""Security"": { ""UsernameToken"": { ""Username"": ""redrobin"", ""Password"": ""fahidsma!"" } }"), new System.Xml.XmlDictionaryReaderQuotas());

}

修改
这种语法的问题是,唯一发布的是:

System.Runtime.Serialization.Json.XmlJsonReader

1 个答案:

答案 0 :(得分:0)

        var request = WebRequest.CreateHttp(myUrl + "/" + uri);
        request.Credentials = new NetworkCredential(myUserName, myPassword, myDomain);
        request.ContentType = "application/json; charset=utf-8";
        request.Method = "POST";

        using (StreamWriter streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            string jsonstring = JsonConvert.SerializeObject(myJsonobject, Formatting.Indented);

            streamWriter.Write(jsonstring);

            streamWriter.Flush();
        }

看看上面