Functions

  • May 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Functions as PDF for free.

More details

  • Words: 282
  • Pages: 2
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; } }

Related Documents

Functions
November 2019 47
Functions
December 2019 49
Functions
May 2020 27
Functions
July 2020 21
Functions
November 2019 36
Functions
November 2019 35