-2

How do I keep authorization after logging in and send messages?

Here is my code:

for (var i = 0; i < Listchat.Length; i++)
{
    string url = Listchat[i] + "/ajax/";
    string par = "act=login&chat=" + "4952" + "&msg=" + log + "&pass=" + pass + "&remember=0&pv=0&c=&bind=0";

    HttpWebRequest request3 = (HttpWebRequest)WebRequest.Create(url);
    request3.CookieContainer = cookieJar;
    request3.UserAgent = "Opera/9.80";
    request3.Method = "POST";
    request3.ContentType = "application/x-www-form-urlencoded";
    byte[] EncodedPostParams3 = Encoding.Default.GetBytes(par);
    request3.ContentLength = EncodedPostParams3.Length;
    request3.GetRequestStream().Write(EncodedPostParams3, 0, EncodedPostParams3.Length);
    request3.GetRequestStream().Close();
    request3.AllowAutoRedirect = false;
    HttpWebResponse response = (HttpWebResponse)request3.GetResponse();


    //SEND MESSAGE!!   Here the message is sent to the chat only 1 time and lost authorization


    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Listchat[i] + "/ajax/?act=send&chat=" +"4952" + "&channel=main&pv=0&msg=" + mess + "");
    req.CookieContainer = cookieJar;
    req.Timeout = 20 * 60 * 1000;
    req.KeepAlive = true;
    HttpWebResponse respons = (HttpWebResponse)req.GetResponse();
    Stream dataStream = respons.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    string responseFromServer = reader.ReadToEnd();
    reader.Close();
    dataStream.Close();
    respons.Close();
    label2.Text = responseFromServer;
}
Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85
user2812337
  • 29
  • 1
  • 6
  • 3
    It appears that you are asking low quality questions. Please make yourself familiar with the FAQ. – usr Sep 24 '13 at 18:43
  • I'm not a native English speaker, I find it hard to understand, please answer – user2812337 Sep 24 '13 at 18:46
  • I have read a lot of things on the stackoverflow, but did not understand – user2812337 Sep 24 '13 at 18:49
  • That it is difficult to give a simple example? What kind of people (( – user2812337 Sep 24 '13 at 18:52
  • 1
    Have you tried sending the response object's cookies back on the next request? That's probably where it's losing authentication because there's no way to know that you're the same user otherwise. As far as the server's concerned you're just a new user sending an un-authorized chat request. – siva.k Sep 24 '13 at 19:01
  • 2 mr.smors and how to send them? – user2812337 Sep 24 '13 at 19:09
  • I feel like there should be some grace for users who don't speak English natively, especially if the user is a new user. 4 down votes seems like a bit much. +1 – Cameron Tinker Sep 24 '13 at 19:30
  • @CameronTinker the problem is not a language barrier. He is not putting the minimum amount of effort in that is required. He is abusing the system. – usr Sep 24 '13 at 19:54
  • @usr I see your point and there could be a little more effort put into the question. I only corrected some grammar and tried to make the question a little more clear. If the OP wants to elaborate more, that's fine. – Cameron Tinker Sep 24 '13 at 19:59
  • @usr,efforts, you say? Yes, I did not sleep day, thinking about it – user2812337 Sep 24 '13 at 20:38

1 Answers1

0

You need a way of passing the cookies from the response to the next request. The problem is that they're not persisting between requests. If you create a CookieContainer and a CookieCollection, you can collect the cookies from the response and pass them to the next request by adding them to the CookieContainer. Here is some modified code that demonstrates this approach:

CookieContainer container = new CookieContainer();
CookieCollection cookies = new CookieCollection();

for (var i = 0; i < Listchat.Length; i++)
{
    string url = Listchat[i] + "/ajax/";
    string par = "act=login&chat=" + "4952" + "&msg=" + log + "&pass=" + pass + "&remember=0&pv=0&c=&bind=0";

    HttpWebRequest request3 = (HttpWebRequest)WebRequest.Create(url);
    request3.CookieContainer = container;
    request3.CookieContainer.Add(cookies);
    request3.UserAgent = "Opera/9.80";
    request3.Method = "POST";
    request3.ContentType = "application/x-www-form-urlencoded";
    byte[] EncodedPostParams3 = Encoding.Default.GetBytes(par);
    request3.ContentLength = EncodedPostParams3.Length;
    request3.GetRequestStream().Write(EncodedPostParams3, 0, EncodedPostParams3.Length);
    request3.GetRequestStream().Close();
    request3.AllowAutoRedirect = false;
    HttpWebResponse response = (HttpWebResponse)request3.GetResponse();
    cookies = response.Cookies;


    //SEND MESSAGE!!   Here the message is sent to the chat only 1 time and lost authorization


    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Listchat[i] + "/ajax/?act=send&chat=" +"4952" + "&channel=main&pv=0&msg=" + mess + "");
    req.CookieContainer = container;
    req.CookieContainer.Add(cookies);
    req.Timeout = 20 * 60 * 1000;
    req.KeepAlive = true;
    HttpWebResponse respons = (HttpWebResponse)req.GetResponse();
    cookies = respons.Cookies;
    Stream dataStream = respons.GetResponseStream();
    StreamReader reader = new StreamReader(dataStream);
    string responseFromServer = reader.ReadToEnd();
    reader.Close();
    dataStream.Close();
    respons.Close();
    label2.Text = responseFromServer;
}
Cameron Tinker
  • 9,634
  • 10
  • 46
  • 85