void Authenticate() { try { if((string.IsNullOrEmpty(username))) { throw new Exception("Username cannot be empty"); } if((string.IsNullOrEmpty(access_key))) { throw new Exception("Access key cannot be empty"); } HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://auth.api.rackspacecloud.com/v1.0");; request.Headers.Add("X-Auth-User",username); request.Headers.Add("X-Auth-Key",access_key); HttpWebResponse response = null; Stream responseStream = null; try { response = (HttpWebResponse)request.GetResponse(); responseStream = response.GetResponseStream(); } catch { throw new Exception("Could not authenticate user. Username or password may be invalid."); } int length = (int)response.ContentLength; // Use Content-Length if between bufSizeMax and bufSizeMin int bufSize = 0; if (length > bufSize) bufSize = length > 4096 ? 4096 : length; // Allocate buffer and StringBuilder for reading response byte[] buf = new byte[bufSize]; StringBuilder sb = new StringBuilder(bufSize); // Read response stream until end while ((length = responseStream.Read(buf, 0, buf.Length)) != 0) sb.Append(Encoding.UTF8.GetString(buf, 0, length)); /**********************For Testing************************/ MessageBox.Show(sb.ToString()); /**********************For Testing************************/ token = response.GetResponseHeader("X-Auth-Token"); X_Storage_Url = response.GetResponseHeader("X-Storage-Url");
}
} catch (Exception ex) { MessageBox.Show("Error: " + ex.Message); return; }
void ReadFile(string path) { try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(X_Storage_Url + "/" + path); request.Method = "GET"; request.Headers.Add("X-Auth-Token",token); HttpWebResponse response = null; Stream responseStream = null; try { response = (HttpWebResponse)request.GetResponse(); responseStream = response.GetResponseStream(); } catch { throw new Exception("Could not read file. File may not exist or token may be invalid."); } int length = (int)response.ContentLength; // Use Content-Length if between bufSizeMax and bufSizeMin int bufSize = 0; if (length > bufSize) bufSize = length > 4096 ? 4096 : length; // Allocate buffer and StringBuilder for reading response byte[] buf = new byte[bufSize]; StringBuilder sb = new StringBuilder(bufSize); // Read response stream until end while ((length = responseStream.Read(buf, 0, buf.Length)) != 0) sb.Append(Encoding.UTF8.GetString(buf, 0, length)); /**********************For Testing************************/ MessageBox.Show(sb.ToString()); /**********************For Testing************************/
} catch(Exception ex) { MessageBox.Show("Error: " + ex.Message); return; } }