你可以检查https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse(v=vs.110).aspx了解如何获得你的回复的httpBody的详细解释。在使用.getResponse()得到响应后,您可以获得响应流,并从变量中读取它的内容。
为纽带的文章中指出:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create (args[0]);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine ("Content length is {0}", response.ContentLength);
Console.WriteLine ("Content type is {0}", response.ContentType);
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader (receiveStream, Encoding.UTF8);
Console.WriteLine ("Response stream received.");
Console.WriteLine (readStream.ReadToEnd());
response.Close();
readStream.Close();
输出将是:
/*
The output from this example will vary depending on the value passed into Main
but will be similar to the following:
Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
...
</html>
*/
有了:
var res = response.GetResponseStream().ReadToEnd();
您将得到JSON体中的字符串表示。在此之后,您可以使用像Newtonsoft.JSON这样的json解析器来解析它。
我希望这回答你的问题至少70%。我认为有很多方法可以编写一个自动分析JSON主体的API(至少在新的net core(mvc vnext)中)。我必须牢记这个方法。