title: Error Forbidden 403 simulating request via C#
body: <p><strong>Scope:</strong></p>
  
  <p>I am developing a C# aplication to simulate queries into <a href="https://www.sefaz.rr.gov.br/sintegra/servlet/hwsintco" rel="nofollow noreferrer">this site</a>. I am quite familiar with simulating web requests for achieving the same human steps, but using code instead.</p>
  
  <p>If you want to try yourself, just type this number into the CNPJ box: 
  <code>08775724000119</code> and write the captcha and click on <code>Confirmar</code></p>
  
  <p>I've dealed with the captcha already, so it's not a problem anymore.</p>
  
  <p><strong>Problem:</strong></p>
  
  <p>As soon as i execute the POST request for a "CNPJ", a exception is thrown:</p>
  
  <blockquote>
    <p>The remote server returned an error: (403) Forbidden.</p>
  </blockquote>
  
  <p><strong>Fiddler Debugger Output:</strong></p>
  
  <p><a href="http://www.fiddler2.com/fiddler2/" rel="nofollow noreferrer">Link for Fiddler Download</a></p>
  
  <p>This is the request generated by my browser, not by my code</p>
  
  <pre class="lang-none prettyprint-override"><code>POST https://www.sefaz.rr.gov.br/sintegra/servlet/hwsintco HTTP/1.1
  Host: www.sefaz.rr.gov.br
  Connection: keep-alive
  Content-Length: 208
  Cache-Control: max-age=0
  Origin: https://www.sefaz.rr.gov.br
  User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko)    Chrome/23.0.1271.97 Safari/537.11
  Content-Type: application/x-www-form-urlencoded
  Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
  Referer: https://www.sefaz.rr.gov.br/sintegra/servlet/hwsintco
  Accept-Encoding: gzip,deflate,sdch
  Accept-Language: pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
  Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
  Cookie: GX_SESSION_ID=gGUYxyut5XRAijm0Fx9ou7WnXbVGuUYoYTIKtnDydVM%3D;   JSESSIONID=OVuuMFCgQv9k2b3fGyHjSZ9a.undefined
  
  
  //    PostData : 
  _EventName=E%27CONFIRMAR%27.&amp;_EventGridId=&amp;_EventRowId=&amp;_MSG=&amp;_CONINSEST=&amp;_CONINSESTG=08775724000119&amp;cfield=rice&amp;_VALIDATIONRESULT=1&amp;BUTTON1=Confirmar&amp;sCallerURL=http%3A%2F%2Fwww.sintegra.gov.br%2Fnew_bv.html
  </code></pre>
  
  <p><strong>Code samples and References used:</strong></p>
  
  <p>I'm using a self developed library to handle/wrap the Post and Get requests.</p>
  
  <p>The request object has the same parameters (Host,Origin, Referer, Cookies..) as the one issued by the browser (logged my fiddler up here). </p>
  
  <p>I've also managed to set the <code>ServicePointValidator</code> of certificates by using:</p>
  
  <pre><code>ServicePointManager.ServerCertificateValidationCallback = 
      new RemoteCertificateValidationCallback (delegate { return true; });
  </code></pre>
  
  <p>After all that configuration, i stil getting the forbidden exception.</p>
  
  <p>Here is how i simulate the request and the exception is thrown</p>
  
  <pre><code>        try
          {
              this.Referer = Consts.REFERER;
  
              // PARAMETERS: URL, POST DATA, ThrownException (bool)
              response = Post (Consts.QUERYURL, postData, true);
          }
          catch (Exception ex)
          {
              string s = ex.Message;
          }
  </code></pre>
  
  <p>Thanks in advance for any help / solution to my problem</p>
  
  <p><strong>Update 1:</strong></p>
  
  <p>I was missing the request for the homepage, which generates cookies (Thanks @W0lf for pointing me that out)</p>
  
  <p>Now there's another weird thing. Fiddler is not showing my Cookies on the request, but here they are :
  <img src="https://i.stack.imgur.com/4h1Ik.png" alt="CookieJar"></p>