在HttpWebRequest“POST”之后接收网页

时间:2015-05-12 17:31:09

标签: c# json httpwebrequest httpwebresponse

我正在执行以下HttpWebRequest:

      private static void InvokeHealthCheckApi()
    {
        var webRequest = (HttpWebRequest)WebRequest.Create(Url);
        string sb = JsonConvert.SerializeObject(webRequest);
        webRequest.Method = "GET";
        webRequest.KeepAlive = true;
        webRequest.AllowAutoRedirect = true;
        webRequest.ContentType = "application/json";
        webRequest.UserAgent =
            "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36";
        webRequest.CookieContainer = cookieJar;

        using (HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse)

        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {

            File.AppendAllText("C:\\httpResponse1.txt", response.Headers.ToString());
            File.AppendAllText("C:\\httpResponse2.html", reader.ReadToEnd());

        }


    }

请求的响应将返回为以下网页: “脚本已禁用。单击”提交“继续。 (提交按钮)

点击提交按钮后,我得到一个提示: “你想从fr74a87d9.work.corp.net打开或保存healthcheck.json(297字节)吗?

点击“打开”按钮后,我收到了我希望收到的json数据。

我的问题是如何解析响应以获取我需要的json数据?将网页作为初始响应并且必须向下钻取以获取json响应是正常的吗? StreamReader无法解析响应,因为它是一个网页而不是json数据。

1 个答案:

答案 0 :(得分:0)