Hello, I've been trying to log in a website for a while but I can't get it working correctly...
Firstly, I'm going to explain what I've done until this moment.
> I opened this website: http://side.utad.pt/cursos/einformatica/ upon which I want to log in.
> After opening its souce code, I found the Url that the
Login form posts to is:
https://side.utad.pt/side-secure3/login.pl
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
http://side.utad.pt/cursos/einformatica/ itself.
> By using tamper data (https://addons.mozilla.org/en-US/firefox/addon/tamper-data/) extension(Firefox) I found that there are 3 post arguments: sessionid, username and password.
To get sessionid, I simply searched for it inside source code and took it from there:
String^ formUrl = "http://side.utad.pt/cursos/einformatica/";
String^ pageSource;
WebClient^ client = gcnew WebClient();
pageSource = client->DownloadString(formUrl);
delete client;
client = nullptr;
int index = pageSource->IndexOf("sessionid");
int startIndex = index + 34;
String^ _sessionid = pageSource->Substring(startIndex, 32);
Until here, everything was fine apart from the Url problem.
> Started formatting all the data gathered(which I believe is the correct way):
String^ formParams;
// format data : username and password are input by user himself
formParams = "sessionid="+ _sessionid+"&username="+username+"&password="+password;
> Started with the "body" of the code:
WebRequest^ req = WebRequest::Create(formUrl);
// encode our data
array<Byte>^ bytes = System::Text::Encoding::ASCII->GetBytes(formParams);
req->ContentType = "application/x-www-form-urlencoded";
req->Method = "POST";
req->ContentLength = bytes->Length;
Stream^ os = req->GetRequestStream();
os->Write(bytes,0,bytes->Length);
os->Close();
Am I doing it correctly until here?
> 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):
// this code is added below os->Close();
WebResponse^ resp = req->GetResponse();
String^ cookieHeader;
cookieHeader = resp->Headers["Set-cookie"]; // saving cookies
WebRequest^ getRequest = WebRequest::Create("http://side.utad.pt/cursos/einformatica/principal");
getRequest->Headers->Add("Cookie", cookieHeader); // using the saved cookies
WebResponse^ getResponse = getRequest->GetResponse();
StreamReader^ sr = gcnew StreamReader(getRequest->GetRequestStream());
pageSource = ""; // reset
pageSource = sr->ReadToEnd();
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'
Secondly and lastly, when that line doesn't raise an exception, this does(cannot send a content-body with this verb-type)
StreamReader^ sr = gcnew StreamReader(getRequest->GetRequestStream());
Any ideas to get this working?
Thanks