9

This piece of code doesn't work; it's logging in into website which is using https protocol. How to solve this problem? The code stops at GetRequestStream() anytime anywhere saying that protocol violation exception is unhandled..

string username = "user";
string password = "pass";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://moje.azet.sk/prihlasenie.phtml?KDE=www.azet.sk%2Findex.phtml%3F");
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)";

Console.WriteLine(request.GetRequestStream());

using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
{
    writer.Write("nick=" + username + "&password=" + password);
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//Retrieve your cookie that id's your session
//response.Cookies

using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
    Console.WriteLine(reader.ReadToEnd());
}
DIF
  • 2,470
  • 6
  • 35
  • 49
Skuta
  • 5,830
  • 27
  • 60
  • 68
  • 2
    I don't have edit priviledges, but you need to obfuscate the username and password variables in your code example. I just tried logging into the site and they got me in. – regex Feb 12 '09 at 16:15
  • Replaced them with "user" and "pass" – Rytmis Feb 12 '09 at 16:19
  • 6
    You should also change your password on the site since the credentials are still in the edit history of this question. – Sean Bright Feb 12 '09 at 16:20
  • Thx ppl, but the account was for test purposes - > I didn't really care if anyone sees it ;-) – Skuta Feb 12 '09 at 16:31
  • @Birgit_B: There is [markdown for that](http://stackoverflow.com/editing-help#code), just two backticks. – H.B. Feb 28 '12 at 18:01

4 Answers4

5

Set request method to post, before calling GetRequestStream

like

request.Method = "POST";

using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
{
    writer.Write("nick=" + username + "&password=" + password);
}
Ramesh
  • 13,043
  • 3
  • 52
  • 88
3

My guess is that the issue you are experiencing is due to the fact (like others have advised) that you are doing a GET request instead of a POST request. Additionally, I noticed that the actual name for the password field on that page is "heslo" and not "password". This typo won't cause the web server to not return a response, but it will cause other issues since the server is looking for that specific variable name to be posted with the password value.

regex
  • 3,585
  • 5
  • 31
  • 41
  • Thx, I got one more issue, not sure if i should make another question.. How do I continue to "get data from different pages from same server using same "loggged cookie and data"?? – Skuta Feb 12 '09 at 16:38
  • 1
    @Skuta: See HttpWebRequest.CookieContainer: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.cookiecontainer.aspx – Rytmis Feb 12 '09 at 16:50
1

You might also want to figure out the total length of what you're posting, beforehand, and set that as the ContentLength of the request. See MSDN:

A ProtocolViolationException is thrown in several cases when the properties set on the HttpWebRequest class are conflicting. This exception occurs if an application sets the ContentLength property and the SendChunked property to true, and then sends an HTTP GET request. This exception occurs if an application tries to send chunked to a server that only supports HTTP 1.0 protocol, where this is not supported. This exception occurs if an application tries to send data without setting the ContentLength property or the SendChunked is false when buffering is disabled and on a keepalive connection (the KeepAlive property is true).

Rytmis
  • 31,467
  • 8
  • 60
  • 69
0

Here is what works for me:

    var request = WebRequest.Create(_url);
    request.PreAuthenticate = true;
    request.Credentials = new NetworkCredential(_userName, _password);
Schultz9999
  • 8,717
  • 8
  • 48
  • 87