0

I try to access a specific page on the site and pull it out of information. I did GET request to the homepage and I get response status code ==OK then I do another GET request to the page that contains the Json I want to retrieve) and the response status code == OK.

Now I want to retrieve the information so I do get request for the resource (another URL that the last page load) And I get the error at this line:

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

"Content-Length or Chunked Encoding cannot be set for an operation that does not write data"

I set all the headers Just like the get request inside the chrome Inspect -> Network Tab -> choose the URL that i wanna(there i can see the get request headers)

this is the code that i run:

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));
            }

I can't understand why is it happening?

EylM
  • 5,196
  • 2
  • 13
  • 24
DanLev
  • 1
  • 4
  • You have a data length of 120 bytes but not writing any data in the request. See : https://stackoverflow.com/questions/29865164/content-length-or-chunked-encoding-cannot-be-set-for-an-operation-that-does-not – jdweng Nov 05 '19 at 11:46
  • @jdweng, what if i dont need to write data? – DanLev Nov 05 '19 at 12:37
  • The don't set the length to 120. Also the ContentType may not be needed. – jdweng Nov 05 '19 at 12:47
  • @jdweng now i get:The remote server returned an error: (403) Forbidden – DanLev Nov 05 '19 at 13:10
  • I'm pretty sure I have set all the headers correct :[ @jdweng – DanLev Nov 05 '19 at 13:12
  • You may need to delete cookies to get rid of old settings. I would try to use a sniffer like wireshark or fiddler and see if a cookie is being sent. If you have working application than compare the 1st request in working with non-working. Is length ZERO? – jdweng Nov 05 '19 at 14:15

0 Answers0