如何迭代动态GET响应

时间:2017-12-28 15:35:20

标签: c#

如果我不知道我将提前获得的结构,我如何迭代GET响应。 让我们说我可以得到这样的回应:

{
    "hostName": "host",
    "domainName": "domain",
}

下次我发送相同的GET请求时,我得到了这个回复:

{
    "hostName": "host",
}

到目前为止我的代码:

public static void GetRequest(string url)
{
    var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "GET";
    String userId = "userID";
    String userPassword = "userPassword";
    NetworkCredential networkCredential = new NetworkCredential(userId, userPassword);
    httpWebRequest.Credentials = networkCredential;
    httpWebRequest.MaximumAutomaticRedirections = 3;
    httpWebRequest.Timeout = 5000;
    System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
    string svcCredentials = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(userId + ":" + userPassword));
    httpWebRequest.Headers.Add("Authorization", "Basic " + svcCredentials);
    Console.WriteLine("Sending HTTP Request");
    var httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    var responseStream = httpWebResponse.GetResponseStream();
    if (responseStream != null)
    {
        var streamReader = new StreamReader(responseStream);
        Console.WriteLine("HTTP Response is: ");
        Console.WriteLine(streamReader.ReadToEnd());
    }
    if (responseStream != null) responseStream.Close();
}

0 个答案:

没有答案