3

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.

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 the first link itself.

» 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.

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
formParams = "sessionid="+ _sessionid+"&username="+username+"&password="+password;

» After that, I started working 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"];

WebRequest^ getRequest = WebRequest::Create("http://side.utad.pt/cursos/einformatica/principal"); // Exception 1
getRequest->Headers->Add("Cookie", cookieHeader);
WebResponse^ getResponse = getRequest->GetResponse();
StreamReader^ sr = gcnew StreamReader(getRequest->GetRequestStream()); // Exception 2
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?

I think that the problem here is related to the cookies. I might have not saved them properly..

Thanks

4

0 回答 0