Skip to content

Instantly share code, notes, and snippets.

@jessgusclark
Last active May 18, 2017 19:17
Show Gist options
  • Save jessgusclark/9ebeaa6a0897b01a01035d35a5106e3f to your computer and use it in GitHub Desktop.
Save jessgusclark/9ebeaa6a0897b01a01035d35a5106e3f to your computer and use it in GitHub Desktop.
API Call with C#
/// <summary>
/// Execute a PULL or GET ApiCall
/// </summary>
/// <param name="url">The full URL including the querystring parameters</param>
/// <param name="type">PULL or GET</param>
/// <param name="cookies">A CookieContainer that can be used to string </param>
/// <returns>Resturns a string of the response from the API call.</returns>
protected string APICall(string url, string type, CookieContainer cookies) {
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = cookies;
request.Method = type;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
String responseString;
using (Stream stream = response.GetResponseStream()) {
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
responseString = reader.ReadToEnd();
}
return responseString;
}
// create new container to be passed from request to request:
CookieContainer cookie = new CookieContainer();
// log into OU Campus:
string response1 = APICall("https://a.cms.omniupdate.com/authentication/login?skin=oucampus&account=UNCO&username=USERNAME&password=PASSWORD", "POST", cookie);
// get results from form with id 104148:
string response2 = APICall("https://a.cms.omniupdate.com/assets/form_results?site=www&asset=104148", "GET", cookie);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment