无法获取正确的编码来从HttpWebResponse中提取正文

时间:2018-08-26 17:28:20

标签: c# api post httpwebrequest httpwebresponse

我正在尝试从政府站点提取数据,该站点在弹出窗口显示时呈现。我检查了网络控制台,并获得了POST请求URL,并能够在Postman上复制请求-响应。现在,我正在尝试以编程方式拨打电话。我尝试使用Postman生成的默认代码,但没有用。

我正在用C#编写代码,我能够获得响应,但是我无法获得正确的编码来提取响应。

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://saubhagya.gov.in/dashboard/data/dashboard_saubhagya");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";
        request.Accept = "application/json, text/javascript, */*; q=0.01";
        request.Host = "saubhagya.gov.in";
        request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36";
        request.Headers.Add("X-Requested-With", "XMLHttpRequest");
        request.Headers.Add("Accept-Encoding: gzip, deflate");
        request.Headers.Add("Accept-Language: en-US,en;q=0.9");
        request.Headers.Add("Cache-Control: no-cache");
        request.Headers.Add("Pragma: no-cache");
        request.Headers.Add("Origin: http://saubhagya.gov.in");
        request.Referer = "http://saubhagya.gov.in/";
        String CookieStr = "_ga=GA1.3.590075225.1533991967; _gid=GA1.3.790472263.1533991967; saubhagyasession=BHxX%2FFfttUfxM7JhoIruGVzdq0m%2F4sGeTn95c%2BUB%2BGvJok9PkS3g9pR8vLVfeEJ1XB8UULGNThvbAeN5HfAu%2FE6qt%2F5X3qL8Yla4my0qmxSmz6Q9ztpLztCD0PyY17uWDnJgkSjSt%2BSF0B5Xh32SUsxBXHH%2BeFGwtIXdAnzSLcxC0MO8KZSiE2io4ksZO6AZ31YSxnGei6CluQzg4fCFgXvVwR4%2F00%2FKAbf0MnhLwaTtXxD0jngmDv3Rjy8enD87c20vwObHGTgcLC3KQoh2lw5L1WRF1lVLlpjzLrUoeJV3cD8o0c15bT5SA%2FV1Y8OqFPhqhpr0%2BzzG%2FbAVs6OKMmLiokl7hHrPx5NECDsmY3KzmCkNHka%2B1ueEWTv%2FTOUqH2hll2A8485gFhqFgnrh%2FKkhOb6I8lChI2QQoyHr%2B9U%3D92add88ce105d8b3ec1dd72efa1dd7ec9b9f1e52";
        CookieContainer cookiecontainer = new CookieContainer();
        string[] cookies = CookieStr.Split(';');
        foreach (string cookie in cookies)
            cookiecontainer.SetCookies(new Uri("http://saubhagya.gov.in/dashboard/data/dashboard_saubhagya"), cookie);
        request.CookieContainer = cookiecontainer;
        using (var streamWriter = new StreamWriter(request.GetRequestStream()))
        {
            string json = "ci_csrf_token=&state=35&district=638&village=645115&vtype=&discom=&search_text=&uuid=&maptype=states&kyroargs=&drilldownkey=&kyroclickid=&kyrorefreshid=&page=dashboard_saubhagya";

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }



        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        using (StreamReader sr = new StreamReader(response.GetResponseStream(), System.Text.Encoding.GetEncoding(response.CharacterSet)))
        {
            var result = sr.ReadToEnd();
        }

我得到一个编码/垃圾字符串作为输出。

请求帮助!

1 个答案:

答案 0 :(得分:0)

在标头中,您声明您接受gzip,但响应并未最终解压缩,因此只需添加:

request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;

然后您可以删除此行,因为标题将自动添加:

//request.Headers.Add("Accept-Encoding: gzip, deflate");