CPE_learningsite
This repository has no backups
This repository's network speed is throttled to 100KB/sec
Upgrade your account to fix these warnings, or use backups.vc for automated backups
4cd176a439ea516368a5d8827cdf8726027259a6
CPE_learningsite / CPE / CPE.App / CPE.App.Web / Connect / Request.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 |
using CPE.App.Web.Code; using System; using System.Collections.Specialized; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Web; using System.Xml; namespace CPE.App.Web.Connect { /// <summary> /// Handles a basic request to the Connect service. /// </summary> public class Request { // Private private XmlDocument _xmlResults; private NameValueCollection _parameters = new NameValueCollection(); private Session _session = new Session(); private static string CleanHost(string url) { if (!url.StartsWith("http")) url = "http://" + url; return url.ToLower(); } /// <summary> /// The action to perform against the service. /// </summary> /// <example>common-info</example> public string Action { get; set; } /// <summary> /// The parameters to pass with the action for this request. /// </summary> public NameValueCollection Parameters { get { return _parameters; } set { _parameters = value; } } /// <summary> /// The Session settings to contact the service. /// </summary> public Session Session { get { return _session; } set { _session = value; } } /// <summary> /// Returns the raw XmlDocument that was returned from the service. /// </summary> public XmlDocument XmlResults { get { return _xmlResults; }} /// <summary> /// Returns the current status of the request. /// </summary> public Status Status { get { // Default Status status = Status.Unknown; // Has a response been received? if(HasResponse) { // Parse the response for the status string statusCode = _xmlResults.SelectSingleNode("//status").Attributes["code"].Value; switch(statusCode) { case "ok": status = Status.OK; break; case "invalid": status = Status.Invalid; break; case "no-access": status = Status.NoAccess; break; case "no-data": status = Status.NoData; break; case "too-much-data": status = Status.TooMuchData; break; } } return status; } } /// <summary> /// Returns whether a response has been received for this request. /// </summary> public bool HasResponse { get { return (_xmlResults != null); } } /// <summary> /// Returns the response headers for this request. Useful for retrieving the session key from a login request. /// </summary> public WebHeaderCollection ResponseHeaders { get; private set; } /// <summary> /// Initializes an empty request. /// </summary> public Request() { } /// <summary> /// Initializes a request. /// </summary> /// <param name="hostname">The hostname to connect to.</param> /// <param name="action">The action to perform.</param> public Request(string hostname, string action) { Session.Hostname = hostname; Action = action; } /// <summary> /// Initializes the request. /// </summary> /// <param name="session">The session object to connect with.</param> /// <param name="action">The action to perform.</param> public Request(Session session, string action) { Session = session; Action = action; } private string _url = null; public string Url { get { return _url; } } /// <summary> /// Sends the request to the service and retrieves the results. /// </summary> /// <returns>Returns true if the request came back or false if an exception was thrown.</returns> public virtual bool Execute() { try { // Example URL: http://connectpro41408666.acrobat.com/api/xml?action=common-info // Build URL var host = CleanHost(Session.Hostname); var url = new StringBuilder(string.Format("{0}/api/xml?action={1}", host, Action)); // Is a session key set? if(Session.SessionKey != null) Parameters.Add("session", Session.SessionKey); // Add parameters to querystring for(int i = 0; i < Parameters.Count; i++) { for(int x = 0; x < Parameters.GetValues(i).Length; x++) { url.AppendFormat("&{0}={1}", HttpUtility.UrlEncode(Parameters.Keys[i]), HttpUtility.UrlEncode(Parameters.GetValues(i)[x])); } } // Ignore SSL validation errors //ServicePointManager.ServerCertificateValidationCallback = IgnoreInvalidSSL; // Retrieve XML string xml = null; using(var request = new WebClient() { UseDefaultCredentials = true, Encoding = Encoding.UTF8 }) { // Is a proxy required? if(Session.Proxy != null) request.Proxy = new WebProxy(Session.Proxy, true) { UseDefaultCredentials = true }; //Extensions.LogServiceCall("[Request][Execute]", String.Format("url = {0}", url.ToString())); try { // System.IO.File.AppendAllText("adobeconnect_log.txt", url.ToString() + Environment.NewLine); } catch { } // Download try { xml = request.DownloadString(url.ToString()); ResponseHeaders = request.ResponseHeaders; } catch(Exception ex) { //var log = new ExceptionLogController(); //log.AddLog(ex, ExceptionLogController.ExceptionLogType.GENERAL_EXCEPTION); //log.AddLog(new Exception(url.ToString()), ExceptionLogController.ExceptionLogType.GENERAL_EXCEPTION); Extensions.LogServiceError("[Request][Execute]", ex); goto theend; } } // Load XML _xmlResults = new XmlDocument(); _xmlResults.LoadXml(xml); _url = url.ToString(); return true; } catch { } theend: _xmlResults = null; ResponseHeaders = null; return false; } /// <summary> /// Overrides the default vertifcation for SSL certificates so that they will always be accepted. /// </summary> private static bool IgnoreInvalidSsl(object sender, System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors errors) { return true; } /// <summary> /// Returns the session key that is returned in the ResponseHeaders from Connect. /// </summary> /// <returns>The session key, if found. Returns an empty string if not found.</returns> public string ExtractSessionKey() { // Default to returning an empty string if the key isn't found string session = string.Empty; // Does the cookie header exist? if(ResponseHeaders != null && ResponseHeaders["Set-Cookie"] != null) { // Example session cookie // BREEZESESSION=Sbreez9u75m9zfau4pi8m8;HttpOnly;domain=.acrobat.com;path=/ // Capture session key var regex = new Regex(@"BREEZESESSION=(?<session>\w*);"); Match match = regex.Match(ResponseHeaders["Set-Cookie"]); if(match.Success) session = match.Groups["session"].Value; } return session; } } /// <summary> /// Specifies the status of the request. /// </summary> public enum Status { Unknown, OK, Invalid, NoAccess, NoData, TooMuchData } public static class ScoTypes { public enum Types { Archive, Attachment, Authorware, Captivate, Content, Course, Curriculum, Event, ExternalEvent, Flv, Folder, Image, Link, Meeting, Presentation, Producer, Session, Swf, Tree, Unknown } public static Types TryParse(string type) { try { switch (type) { case "archive": return Types.Archive; case "attachment": return Types.Attachment; case "authorware": return Types.Authorware; case "captivate": return Types.Captivate; case "content": return Types.Content; case "course": return Types.Course; case "curriculum": return Types.Curriculum; case "event": return Types.Event; case "external-event": return Types.ExternalEvent; case "flv": return Types.Flv; case "folder": return Types.Folder; case "image": return Types.Image; case "link": return Types.Link; case "meeting": return Types.Meeting; case "presentation": return Types.Presentation; case "producer": return Types.Producer; case "session": return Types.Session; case "swf": return Types.Swf; case "tree": return Types.Tree; default: return Types.Content; } } catch (Exception) { } return Types.Unknown; } } } |
Commits for CPE_learningsiteCPE/CPE.App/CPE.App.Web/Connect/Request.cs
Revision | Author | Commited | Message |
---|---|---|---|
4cd176 ... | v.shishlov | Fri 27 Aug, 2021 14:33:17 +0000 | initial commit |