title: logging in to a website using Httpwebrequest
body: <p>I'm new to programming (C#) and I started writing a bot for a MMORPG to learn communicating with websites programmatically and stuff...</p>
  
  <p>I found many questions about this topic in here and there. At last with a helping hand,I could find something and complete it, which unfortunately ain't working :(</p>
  
  <p>The mothods:</p>
  
  <pre><code>    public static CookieCollection GetCookie(HttpWebRequest request)
      {
  
          if (request.CookieContainer == null)
          { return new CookieContainer().GetCookies(request.RequestUri); }
          else
          { return request.CookieContainer.GetCookies(request.RequestUri); }
      }
  
  
      public static CookieContainer GetCookie(HttpWebResponse response)
      {
  
          CookieContainer cookiecontainer = new CookieContainer();
          cookiecontainer.Add(response.Cookies);
  
          return cookiecontainer;
      }
  
  
      public static void SetCookie(HttpWebRequest request, CookieContainer cookie)
      {
          request.CookieContainer = cookie;
      }
  
  
      public static void SetCookie(HttpWebResponse response, CookieCollection cookie)
      {
          response.Cookies = cookie;
      }
  
  
      public static HttpWebResponse PostData(string uri,string request,CookieContainer cookie)
      {
          HttpWebRequest httprequest;
          byte[] requestbytes;
          Stream requeststream;
          HttpWebResponse httpresponse;
  
          httprequest = (HttpWebRequest)HttpWebRequest.Create(uri);
  
          if (cookie == null)
          {httprequest.CookieContainer=new CookieContainer();}
          else
          {httprequest.CookieContainer=cookie;}
  
          httprequest.Method = "POST";
          httprequest.ContentType = "application/x-www-form-urlencoded";
          httprequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19";
          requestbytes = ASCIIEncoding.ASCII.GetBytes(request);
          httprequest.ContentLength = requestbytes.Length;
  
          requeststream=httprequest.GetRequestStream();
          requeststream.Write(requestbytes,0,requestbytes.Length);
          requeststream.Close();
  
          httpresponse=(HttpWebResponse)httprequest.GetResponse();
  
          if (!(httpresponse.Cookies.Count&gt;0))
          { SetCookie(httpresponse, GetCookie(httprequest)); }
  
          return httpresponse;
      }
  
  
      public static HttpWebResponse GetData(string uri, CookieContainer cookie)
      {
          HttpWebRequest httprequest;
          HttpWebResponse httpresponse;
  
          httprequest = (HttpWebRequest)HttpWebRequest.Create(uri);
  
          if (cookie == null)
          { httprequest.CookieContainer = new CookieContainer(); }
          else
          { httprequest.CookieContainer = cookie; }
  
          httprequest.Method = "GET";
          httprequest.ContentType = "application/x-www-form-urlencoded";
          httprequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.142 Safari/535.19";
  
          httpresponse = (HttpWebResponse)httprequest.GetResponse();
  
          if (!(httpresponse.Cookies.Count &gt; 0))
          { SetCookie(httpresponse, GetCookie(httprequest)); }
  
          return httpresponse;
      }
  </code></pre>
  
  <p>the Main:</p>
  
  <pre><code>        string uri = "http://s2.kingsera.org";
          string userName = "someUserName";
          string passWord = "somePassWord";
  
          string postData = "signinUsername=" + userName + "&amp;signinPassword=" + passWord + "&amp;signinRemember=remember";
          CookieContainer cookie = new CookieContainer();
  
          HttpWebResponse response = PostData(uri, postData, cookie);
  
          CookieContainer c = GetCookie(response);
  </code></pre>
  
  <p>It seems the login page is doing something to make it more complicated! <a href="http://s2.kingsera.org" rel="nofollow">http://s2.kingsera.org</a></p>
  
  <p>Every single comment/suggestion will be appreciated.
  Thanks in advance.</p>