Content-Length или Chunked Encoding не могут быть установлены для операции, которая не записывает данные. Что мне делать? - PullRequest
0 голосов
/ 05 ноября 2019

Я пытаюсь получить доступ к определенной странице на сайте и извлечь ее из информации. Я сделал GET запрос на домашнюю страницу и получил код состояния ответа == ОК, затем я сделал еще один GET запрос на страницу, содержащую Json Я хочу получить) и код состояния ответа == ОК.

Теперь я хочу получить информацию, чтобы я получил запрос на ресурс (другой URL, который загружается последней страницей), и я получаю сообщение об ошибке в этой строке:

HttpWebResponse oHttpResponseIndicesApiUrl = (HttpWebResponse)oHttpRequestIndicesApiUrl.GetResponse();

«Content-Length или Chunked Encoding нельзя установить для операции, которая не записывает данные»

Я установил все заголовки Так же, как запрос get внутри Chrome Inspect -> вкладка Network -> выберитеURL, который я хочу (там я вижу заголовки запроса get)

это код, который я запускаю:

HttpWebRequest oHttpRequestIndicesApiUrl = (HttpWebRequest)WebRequest.Create(sIndicesApiURL);
            LOG.DebugFormat("{0}:calculateIndexSecurityWeights(), Create get request to '{0}'", Name, sIndicesApiURL);
            oHttpRequestIndicesApiUrl.CookieContainer = new CookieContainer();
            foreach (Cookie oCookie in oHttpResponseIndicesParmsUrl.Cookies)
            {
                oHttpRequestIndicesApiUrl.CookieContainer.Add(oCookie);
            }
            oHttpRequestIndicesApiUrl.AllowAutoRedirect = false;
            oHttpRequestIndicesApiUrl.Accept = ("application/json, text/plain, */*");
            oHttpRequestIndicesApiUrl.Headers.Add("accept-encoding", "gzip, deflate, br");
            oHttpRequestIndicesApiUrl.Headers.Add("accept-language", "he-IL");
            oHttpRequestIndicesApiUrl.KeepAlive = true;
            oHttpRequestIndicesApiUrl.ContentLength = 120;
            oHttpRequestIndicesApiUrl.ContentType = "application/json;charset=UTF-8";
            oHttpRequestIndicesApiUrl.Host = "api.tase.co.il";
            oHttpRequestIndicesApiUrl.Headers.Add("origin", "https://www.tase.co.il");
            oHttpRequestIndicesApiUrl.Referer = sIndicesParamsURL;
            oHttpRequestIndicesApiUrl.Headers.Add("sec-fetch-mode", "cors");
            oHttpRequestIndicesApiUrl.Headers.Add("sec-fetch-site", "same-site");
            oHttpRequestIndicesApiUrl.Headers.Add("upgrade-insecure-requests", "1");
            oHttpRequestIndicesApiUrl.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36";

            LOG.DebugFormat("{0}:calculateIndexSecurityWeights(), Set headers to '{1}'", Name, sIndicesApiURL);
            HttpWebResponse oHttpResponseIndicesApiUrl = (HttpWebResponse)oHttpRequestIndicesApiUrl.GetResponse();
            if (oHttpResponseIndicesApiUrl.StatusCode != HttpStatusCode.OK)
            {
                // response failed 
                throw new ApplicationException(string.Format("get response from url '{0}' failed, Status Code: '{1}', Status Description '{2}'", sIndicesApiURL, oHttpResponseIndicesApiUrl.StatusCode, oHttpResponseIndicesApiUrl.StatusDescription));
            }

Я не могу понять, почему это происходит?

...