Advertisement
Guest User

Untitled

a guest
Oct 4th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Security.Cryptography;
  6. using System.IO;
  7. using System.Net;
  8. using System.Xml.Linq;
  9. using System.Xml;
  10. using System.Windows.Forms;
  11.  
  12.  
  13. namespace SG.Colonies
  14. {
  15.     public class Colonies
  16.     {
  17.  
  18.  
  19.  
  20.         public void loginConnection(string username, string password)
  21.         {
  22.  
  23.             WebRequest req = WebRequest.Create(Server.loginAddress);
  24.             req.Method = "POST";
  25.             req.ContentType = "application/x-www-form-urlencoded";
  26.  
  27.             string postData = "username=" + username + "&password=" + password;
  28.             byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  29.             req.ContentLength = byteArray.Length;
  30.  
  31.             Stream ds = req.GetRequestStream();
  32.             ds.Write(byteArray, 0, byteArray.Length);
  33.             ds.Close();
  34.  
  35.             WebResponse wr = req.GetResponse();
  36.             ds = wr.GetResponseStream();
  37.             StreamReader reader = new StreamReader(ds);
  38.             XDocument doc;
  39.  
  40.             try
  41.             {
  42.                 string receive;
  43.                 doc = XDocument.Load(reader);
  44.                 receive = doc.ToString();
  45.                 User.logged = 1;
  46.  
  47.                 if (User.logged == 1)
  48.                 {
  49.                     // mainUser.username = from c in doc.Descendants("root").Descendants("user") select c.Element("username");
  50.                     var q = from c in doc.Descendants("root").Descendants("user") select (string)c.Element("username");
  51.                     foreach (string name in q)
  52.                         User.username = name;
  53.  
  54.  
  55.                     q = from c in doc.Descendants("root").Descendants("user") select (string)c.Element("email");
  56.                     foreach (string email in q)
  57.                         User.email = email;
  58.  
  59.                 }
  60.  
  61.             }
  62.             catch (Exception ex)
  63.             {
  64.                 MessageBox.Show(ex.ToString());
  65.             }
  66.  
  67.  
  68.  
  69.         }
  70.  
  71.         public void regConn(string username, string password, string email, string planet)
  72.         {
  73.  
  74.             WebRequest req = WebRequest.Create(Server.registerAddress);
  75.  
  76.             req.Method = "POST";
  77.             req.ContentType = "application/x-www-form-urlencoded";
  78.  
  79.             string postData = "username=" + username + "&password=" + password +
  80.                 "&email=" + email + "&planet=" + planet;
  81.             byte[] byteArray = Encoding.UTF8.GetBytes(postData);
  82.  
  83.             req.ContentLength = byteArray.Length;
  84.  
  85.  
  86.             Stream ds = req.GetRequestStream();
  87.             ds.Write(byteArray, 0, byteArray.Length);
  88.             ds.Close();
  89.  
  90.             WebResponse wr = req.GetResponse();
  91.             //label1.Content = ((HttpWebResponse)wr).StatusDescription;
  92.             ds = wr.GetResponseStream();
  93.             StreamReader reader = new StreamReader(ds);
  94.             XDocument doc;
  95.  
  96.             try
  97.             {
  98.  
  99.                 doc = XDocument.Load(reader);
  100.                 string receive;
  101.                 receive = doc.ToString();
  102.  
  103.             }
  104.             catch (Exception ex)
  105.             {
  106.                 MessageBox.Show(ex.ToString());
  107.                 MessageBox.Show("Probably the username or email is already taken! Please try another one!", "Register Error", MessageBoxButtons.OK);
  108.  
  109.             }
  110.  
  111.  
  112.         }
  113.  
  114.  
  115.         public static string MD5Encrypt(string value)
  116.         {
  117.             string ret = String.Empty;
  118.             MD5CryptoServiceProvider md5H = new MD5CryptoServiceProvider();
  119.  
  120.             byte[] data = System.Text.Encoding.ASCII.GetBytes(value);
  121.             data = md5H.ComputeHash(data);
  122.  
  123.             for (int i = 0; i < data.Length; i++)
  124.             {
  125.                 ret += data[i].ToString("x2").ToLower();
  126.             }
  127.             return ret;
  128.         }
  129.  
  130.  
  131.  
  132.     }
  133. }
  134.    
  135.         /*
  136.         public void xdoc(StreamReader read)
  137.         {
  138.            
  139.             var q = from c in doc.Descendants("site") select (string)c.Element("name");
  140.             foreach (string name in q)
  141.             {
  142.                 username[i] = name;
  143.                 i++;
  144.             }
  145.            
  146.            
  147.         }
  148.         */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement