title: Having problems when trying to login to a website using C++/cli
body: <p>I've been roughly searching over the internet for some answers to my problem, but I still couldn't figure out how to log in to a website properly. </p>
  
  <p>Firstly, I'm going to explain what I've done until this moment.</p>
  
  <p>» I opened this website: <a href="http://side.utad.pt/cursos/einformatica/" rel="nofollow">http://side.utad.pt/cursos/einformatica/</a> upon which I want to log in.</p>
  
  <p>» After opening its souce code, I found the Url that the Login form posts to is:</p>
  
  <p><a href="https://side.utad.pt/side-secure3/login.pl" rel="nofollow">https://side.utad.pt/side-secure3/login.pl</a></p>
  
  <p>I tried to open it but an Internal Error came out so I tried to access that url without /login.pl instead but i don't have permission to. As I can't get this Url working, I thought about using the first link itself.</p>
  
  <p>» By using tamper data extension(Firefox) I found that there are 3 post arguments: sessionid, username and password. Username and password are input by the user himself.</p>
  
  <p>To get sessionid, I simply searched for it inside source code and took it from there:</p>
  
  <pre><code>String^ formUrl = "http://side.utad.pt/cursos/einformatica/"; 
  String^ pageSource;
  
  WebClient^ client = gcnew WebClient();
  pageSource = client-&gt;DownloadString(formUrl);
  delete client;
  client = nullptr;int index = pageSource-&gt;IndexOf("sessionid");
  int startIndex = index + 34;
  String^ _sessionid = pageSource-&gt;Substring(startIndex, 32);
  </code></pre>
  
  <p>Until here, everything was fine apart from the Url problem.</p>
  
  <p>» Started formatting all the data gathered(which I believe is the correct way):</p>
  
  <pre><code>String^ formParams; 
  // format data
  formParams = "sessionid="+ _sessionid+"&amp;username="+username+"&amp;password="+password;
  </code></pre>
  
  <p>» After that, I started working with the "body" of the code:</p>
  
  <pre><code>WebRequest^ req = WebRequest::Create(formUrl);
  // encode our data
  array&lt;Byte&gt;^ bytes = System::Text::Encoding::ASCII-&gt;GetBytes(formParams);
  
  req-&gt;ContentType = "application/x-www-form-urlencoded";
  req-&gt;Method = "POST";
  req-&gt;ContentLength = bytes-&gt;Length;
  
  Stream^ os = req-&gt;GetRequestStream();
  os-&gt;Write(bytes,0,bytes-&gt;Length);
  os-&gt;Close();
  </code></pre>
  
  <p>Am I doing it correctly until here?</p>
  
  <p>» I wanted to check if i'm logged in or not, so I thought about getting another source code, but this time on pos-login page(can be accessed without logging in but we're always carried to that page after logging in):</p>
  
  <pre><code>// this code is added below os-&gt;Close();
  WebResponse^ resp = req-&gt;GetResponse(); 
  String^ cookieHeader;
  cookieHeader = resp-&gt;Headers["Set-cookie"];
  
  WebRequest^ getRequest = WebRequest::Create("http://side.utad.pt/cursos/einformatica/principal"); // Exception 1
  getRequest-&gt;Headers-&gt;Add("Cookie", cookieHeader);
  WebResponse^ getResponse = getRequest-&gt;GetResponse();
  StreamReader^ sr = gcnew StreamReader(getRequest-&gt;GetRequestStream()); // Exception 2
  pageSource = ""; // reset
  pageSource = sr-&gt;ReadToEnd();
  </code></pre>
  
  <p>Firstly, the first line does raise an exception - most of the times - but I don't know the cause: 'The server commited a protocol violation Section=ResponseStatusLine'</p>
  
  <p>Secondly and lastly, when that line doesn't raise an exception, this does(cannot send a content-body with this verb-type)</p>
  
  <pre><code>StreamReader^ sr = gcnew StreamReader(getRequest-&gt;GetRequestStream());
  </code></pre>
  
  <p>Any ideas to get this working?</p>
  
  <p>I think that the problem here is related to the cookies. I might have not saved them properly..</p>
  
  <p>Thanks</p>