public Hashtable Login(string username, string password, string url) {
// Set the base URI - this will be used for every other server call
baseURI = url;
string uri = "/designer/login.ws";
Hashtable data = new Hashtable();
// Create parameters for post
StringBuilder sb = new StringBuilder();
sb.Append("username=").Append(username).Append("&password=").Append(password);
// Make sure URI is valid
try {
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(baseURI + uri);
} catch (Exception e) {
MessageBox.Show("The Server URL provided is invalid.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
return null;
}
XmlDocument doc = CallServer(sb.ToString(), uri);
if (doc == null) {
data.Add(Globals.RETURN_CODE, -1);
return data;
}
// Return Code
int code = ParseReturnCode(doc);
data.Add(Globals.RETURN_CODE, code);
if (code < 0) {
MessageBox.Show("Error parsing return data from Server.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
} else if (code == 0) {
MessageBox.Show("Incorrect username and/or password. Please try again.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
} else if (code == 1) {
// Parse User Info
Owner owner = new Owner();
XmlElement user = GetXmlElement(doc, "user");
owner.ID = ParseIntValue(user, "id", 0);
owner.Name = ParseStringValue(user, "name");
owner.ContentAdmin = Boolean.Parse(ParseStringValue(user, "admin"));
// Parse databases
databases = new List<Database>();
XmlNodeList list = doc.GetElementsByTagName("database");
for (int i = 0; i < list.Count; i++) {
XmlElement elm = (XmlElement)list[i];
Database db = new Database();
db.ID = ParseIntValue(elm, "id", 0);
db.Name = ParseStringValue(elm, "name");
db.Description = ParseStringValue(elm, "description");
db.DataSourceName = ParseStringValue(elm, "ds-id");
databases.Add(db);
}
// Parse audience info
List<Audience> audiences = new List<Audience>();
list = doc.GetElementsByTagName("audience");
for (int i = 0; i < list.Count; i++) {
XmlElement elm = (XmlElement)list[i];
Audience aud = new Audience();
aud.ID = ParseIntValue(elm, "id", 0);
aud.Name = ParseStringValue(elm, "name");
audiences.Add(aud);
}
owner.Audiences = audiences;
data.Add(Globals.AUDIENCES, audiences);
data.Add(Globals.DATABASES, databases);
data.Add(Globals.USER, owner);
} else if (code == 2) {
MessageBox.Show("You do not have the required capabilities to use Designer", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return data;
}
private XmlDocument CallServer(string postdata, string url) {
HttpWebRequest req = null;
HttpWebResponse res = null;
Stream os = null;
string uri = baseURI + url;
XmlDocument doc = null;
try {
// Create parameters for post
if (database != null) {
if ((postdata == null) || (postdata.Trim().Length == 0)) {
postdata = "ds=" + database.DataSourceName;
} else {
postdata += "&ds=" + database.DataSourceName;
}
} else {
if (postdata == null)
postdata = "";
}
//byte[] bytes = Encoding.ASCII.GetBytes(postdata);
byte[] bytes = Encoding.UTF8.GetBytes(postdata);
// send the POST
req = (HttpWebRequest)HttpWebRequest.Create(uri);
if (cookieContainer == null) cookieContainer = new CookieContainer();
req.CookieContainer = cookieContainer;
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
req.Method = "POST";
req.ContentLength = bytes.Length; //Count bytes to send
os = req.GetRequestStream();
os.Write(bytes, 0, bytes.Length); //Send it
os.Close();
// Get the Response
res = (HttpWebResponse)req.GetResponse();
if (res == null) {
MessageBox.Show("Could not establish a connection to the Server.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
return doc;
}
//XmlTextReader reader = new XmlTextReader(res.GetResponseStream());
//cookieContainer = req.CookieContainer;
doc = new XmlDocument();
doc.Load(res.GetResponseStream());
} catch (Exception ex) {
//MessageBox.Show("Error communicating with Coachware Server." + ex.ToString(), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
} finally {
if (os != null)
os.Close();
if (res != null)
res.Close();
}
return doc;
}
}
Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.
When asked, what has been your best career decision?
Deciding to stick with EE.
Being involved with EE helped me to grow personally and professionally.
Connect with Certified Experts to gain insight and support on specific technology challenges including:
We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE