/// <summary> /// Constructs an object for an asynchronous request state. /// </summary> /// <param name="uri">The URI of the asynchronous request.</param> /// <param name="callback">The callback function for the asynchronous request.</param> /// <param name="userState">The user state.</param> public AsyncWebResult(Uri uri, AsyncWebRequestCallback callback, object userState = null) : base(userState) { this.SendData = new AsyncWebBuffer(); this.ReceiveData = new AsyncWebBuffer(); this.Request = (HttpWebRequest)WebRequest.Create(uri); this.Callback = callback; this.buffer = new byte[BUFFER_SIZE]; }
/// <summary> /// Begins a new request to the specified CDN Finder server. /// </summary> /// <param name="uri">The CDN Finder URI.</param> /// <param name="sites">The list of web sites.</param> /// <param name="callback">The callback method.</param> /// <param name="userState">The user state.</param> /// <returns>The result of the asynchronous web operation.</returns> public IAsyncResult Begin(Uri uri, IEnumerable<object> sites, AsyncWebRequestCallback callback, object userState = null) { // Create the request state. AsyncWebResult asyncState = new AsyncWebResult(uri, callback, userState); // Generate the request boundary. string boundary = "----InetAnalytics{0}".FormatWith(AsyncWeb.GenerateNonce()); // Set the request headers. asyncState.Request.Method = "POST"; asyncState.Request.Accept = "text/html,application/xhtml+xml,application/xml"; asyncState.Request.ContentType = "multipart/form-data; boundary={0}".FormatWith(boundary); asyncState.Request.Timeout = this.config.Timeout; asyncState.Request.AllowAutoRedirect = this.config.AutoRedirect; // Compute the send data. StringBuilder builder = new StringBuilder(); builder.AppendFormat("--{0}{1}", boundary, Environment.NewLine); builder.AppendLine("Content-Disposition: form-data; name=\"file\"; filename=\"sites\""); builder.AppendLine("Content-Type: text/plain"); builder.AppendLine(); foreach (object site in sites) { if (!string.IsNullOrWhiteSpace(site.ToString())) { builder.AppendLine(site.ToString()); } } builder.AppendFormat("--{0}{1}", boundary, Environment.NewLine); builder.AppendLine("Content-Disposition: form-data; name=\"format\""); builder.AppendLine(); builder.AppendLine("xml"); builder.AppendFormat("--{0}--{1}", boundary, Environment.NewLine); // Append the send data. asyncState.SendData.Append(builder.ToString(), Encoding.UTF8); // Begin the request. return base.Begin(asyncState); }
/// <summary> /// Begins a new request to the specified Mercury server to upload a session. /// </summary> /// <param name="uri">The Mercury server URI.</param> /// <param name="id">The session identifier.</param> /// <param name="author">The session author.</param> /// <param name="description">The session description.</param> /// <param name="timestamp">The session timestamp.</param> /// <param name="callback">The callback method.</param> /// <param name="userState">The user state.</param> /// <returns>The result of the asynchronous web operation.</returns> public IAsyncResult BeginUploadSession(Uri uri, Guid id, string author, string description, DateTime timestamp, AsyncWebRequestCallback callback, object userState = null) { // Create the request state. AsyncWebResult asyncState = new AsyncWebResult(uri, callback, userState); // Set the request headers. asyncState.Request.Method = "POST"; asyncState.Request.Accept = "text/html,application/xhtml+xml,application/xml"; asyncState.Request.ContentType = "application/json;charset=UTF-8"; // Create the traceroute JSON object. JObject obj = new JObject( new JProperty("sessionId", id.ToString()), new JProperty("author", author), new JProperty("description", description), new JProperty("dateStart", timestamp.ToUniversalTime().ToString(@"yyyy-MM-ddTHH:mm:ss.fffZ")) ); // Append the send data. asyncState.SendData.Append(obj.ToString(), Encoding.UTF8); // Begin the request. return base.Begin(asyncState); }
/// <summary> /// Begins a new request to the specified Mercury server to upload a traceroute. /// </summary> /// <param name="uri">The Mercury server URI.</param> /// <param name="traceroute">The traceroute to upload.</param> /// <param name="callback">The callback method.</param> /// <param name="userState">The user state.</param> /// <returns>The result of the asynchronous web operation.</returns> public IAsyncResult BeginUploadTraceroute(Uri uri, MercuryTraceroute traceroute, AsyncWebRequestCallback callback, object userState = null) { // Create the request state. AsyncWebResult asyncState = new AsyncWebResult(uri, callback, userState); // Set the request headers. asyncState.Request.Method = "POST"; asyncState.Request.Accept = "text/html,application/xhtml+xml,application/xml"; asyncState.Request.ContentType = "application/json;charset=UTF-8"; // Create the traceroute JSON object. JObject obj = new JObject( new JProperty("sessionId", traceroute.Id.ToString()), new JProperty("srcIp", traceroute.SourceIp != null ? traceroute.SourceIp.ToString() : "none"), new JProperty("dstIp", traceroute.DestinationIp.ToString()), new JProperty("srcName", traceroute.SourceHostname), new JProperty("dstName", traceroute.DestinationHostname), new JProperty("hops", new JArray(from hop in traceroute.Hops select new JObject( new JProperty("id", hop.Number.ToString()), new JProperty("ip", hop.Address != null ? hop.Address.ToString() : "none"), new JProperty("asn", hop.AutonomousSystems != null ? new JArray(from asn in hop.AutonomousSystems select asn.ToString()) : new JArray()), new JProperty("rtt", hop.Rtt != null ? new JArray(from rtt in hop.Rtt select rtt.ToString()) : new JArray()) ) ) )); // Append the send data. asyncState.SendData.Append(obj.ToString(), Encoding.UTF8); // Begin the request. return base.Begin(asyncState); }