且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JSON格式不正确,响应错误

更新时间:2022-10-22 11:39:51

也许,您对代码进行了一次try/catch,以便您可以更好地了解原因.

I am attempting to create a c# console app and send a JSON message to a site, and get the response back.  My data appears to send okay, but I get an error when attempting to read the response.

What do I need to alter in order to be able to read the response and determine if good or bad?

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());

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

Maybe, you put a try/catch around the code so that you might get better information as to the why.