SHOW:
         |
         |
         - or go back to the newest paste.    
    | 1 | using System; | |
| 2 | using System.Collections.Generic; | |
| 3 | using System.Linq; | |
| 4 | using System.Text; | |
| 5 | using System.Net; | |
| 6 | using System.Xml.Linq; | |
| 7 | using System.IO; | |
| 8 | ||
| 9 | - | namespace ConsoleApplication1 | 
| 9 | + | namespace APIHelper | 
| 10 | {
 | |
| 11 | public static class APITool | |
| 12 |     {
 | |
| 13 | /// <summary> | |
| 14 | /// Generic utility method to build a query string from a dictionary | |
| 15 | /// </summary> | |
| 16 | /// <typeparam name="TKey">Query string variable to be assigned a value</typeparam> | |
| 17 | /// <typeparam name="TValue">Query string value to be assigned to the key</typeparam> | |
| 18 | /// <param name="dict">Dictionary<TKey, TValue> to be passed to the program</param> | |
| 19 | /// <returns>Returns a completed query string, in the form key1=value1&key2=value2</returns> | |
| 20 | public static string BuildQueryString<TKey,TValue>(Dictionary<TKey, TValue> dict) | |
| 21 |         {
 | |
| 22 | //empty query string | |
| 23 | string queryString = ""; | |
| 24 | //loop over a key:value pair (hashtable, dict, etc) and build a query string | |
| 25 | foreach (KeyValuePair<TKey, TValue> d in dict) | |
| 26 |             {
 | |
| 27 | queryString += d.Key + "=" + d.Value + "&"; | |
| 28 | } | |
| 29 | //Remove trailing & character | |
| 30 | return queryString.Substring(0,queryString.Length-1); | |
| 31 | } | |
| 32 | ||
| 33 | /// <summary> | |
| 34 | /// Sends a GET request to the URI passed to the constructor. | |
| 35 | /// </summary> | |
| 36 | /// <param name="queryString">Accepts an optional parameter, queryString, which contains a query string of values</param> | |
| 37 | /// <returns>Returns a response from the webserver as a string</returns> | |
| 38 | public static string GET(string uri, string queryString=null) | |
| 39 |         {
 | |
| 40 | WebClient getRequest = new WebClient(); | |
| 41 | return getRequest.DownloadString(new Uri(uri + "?" + queryString)); | |
| 42 | } | |
| 43 | ||
| 44 | /// <summary> | |
| 45 | /// Sends a POST request to the URI passed to the constructor | |
| 46 | /// </summary> | |
| 47 | /// <param name="queryString">Accepts optional parameter, queryString, containing a query string to be sent to server</param> | |
| 48 | /// <returns>Returns response from webserver as a string</returns> | |
| 49 | public static string POST(string uri, string queryString = null) | |
| 50 |         {
 | |
| 51 | //New HTTP Request to the URI | |
| 52 | HttpWebRequest postRequest = (HttpWebRequest)WebRequest.Create(new Uri(uri)); | |
| 53 | //Response from server | |
| 54 | string response; | |
| 55 | ||
| 56 | //Convert query string to UTF-8 | |
| 57 | byte[] data = Encoding.ASCII.GetBytes(queryString); | |
| 58 | ||
| 59 | //Set up headers of post request | |
| 60 | - | Stream stream = postRequest.GetRequestStream(); | 
| 60 | + | |
| 61 | - | stream.Write(data, 0, data.Length); | 
| 61 | + | |
| 62 | - | stream.Close(); | 
| 62 | + | |
| 63 | //Open a stream to write the data | |
| 64 | using (Stream stream = postRequest.GetRequestStream()) | |
| 65 | - | HttpWebResponse HTTPResponse = (HttpWebResponse)postRequest.GetResponse(); | 
| 65 | + | |
| 66 | - | stream = HTTPResponse.GetResponseStream(); | 
| 66 | + | stream.Write(data, 0, data.Length); | 
| 67 | - | StreamReader reader = new StreamReader(stream); | 
| 67 | + | |
| 68 | - | string response = reader.ReadToEnd(); | 
| 68 | + | |
| 69 | using (HttpWebResponse HTTPResponse = (HttpWebResponse)postRequest.GetResponse()) | |
| 70 | - | //Clean up streams | 
| 70 | + | using (Stream stream = HTTPResponse.GetResponseStream()) | 
| 71 | - | reader.Close(); | 
| 71 | + | using (StreamReader reader = new StreamReader(stream)) | 
| 72 | - | stream.Close(); | 
| 72 | + | |
| 73 | - | HTTPResponse.Close(); | 
| 73 | + | response = reader.ReadToEnd(); | 
| 74 | } | |
| 75 | ||
| 76 | return response; | |
| 77 | ||
| 78 | ||
| 79 | } | |
| 80 | ||
| 81 | } | |
| 82 | } | 
