Last active
May 18, 2017 19:17
-
-
Save jessgusclark/9ebeaa6a0897b01a01035d35a5106e3f to your computer and use it in GitHub Desktop.
API Call with C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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