/// <summary> /// Remote API method execution using HTTP "post" method. /// </summary> private DataType ExecutePostHttpRequest <ContractType, DataType>(ViddlerMethodAttribute methodAttribute, StringDictionary parameters) { StringBuilder requestPath = new StringBuilder(); StringBuilder requestData = new StringBuilder(); requestPath.Append((this.EnableSsl && methodAttribute.IsSecure) ? this.SecureBaseUrl : this.BaseUrl); requestPath.Append(methodAttribute.MethodName); requestPath.Append(".xml"); requestData.Append("key="); requestData.Append(this.ApiKey); if (methodAttribute.IsSessionRequired) { requestData.Append("&sessionid="); requestData.Append(this.SessionId); } if (parameters != null && parameters.Keys.Count > 0) { foreach (string key in parameters.Keys) { requestData.Append("&"); requestData.Append(key); requestData.Append("="); requestData.Append(ViddlerHelper.EncodeRequestData(parameters[key])); } } byte[] postDataBytes = System.Text.Encoding.UTF8.GetBytes(requestData.ToString()); try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestPath.ToString()); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.UserAgent = string.Concat("Microsoft .NET, ", this.GetType().Assembly.FullName); request.AllowWriteStreamBuffering = true; request.Accept = "text/xml"; request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; request.KeepAlive = true; request.Timeout = int.MaxValue; request.ReadWriteTimeout = int.MaxValue; using (Stream requestStream = request.GetRequestStream()) { requestStream.Write(postDataBytes, 0, postDataBytes.Length); requestStream.Flush(); } DataType responseObject = ViddlerService.HandleHttpResponse <ContractType, DataType>(request, methodAttribute, this.DumpFolder); return(responseObject); } catch (System.Net.WebException exception) { throw ViddlerService.HandleHttpError(exception); } }
/// <summary> /// Remote API method execution using HTTP "post" method with content type set to "multipart/form-data". /// </summary> private DataType ExecuteMultipartHttpRequest <ContractType, DataType>(ViddlerMethodAttribute methodAttribute, StringDictionary parameters, string fileName, Stream fileStream, Data.UploadEndPoint endPoint) { StringBuilder requestPath = new StringBuilder(); if (endPoint != null && !string.IsNullOrEmpty(endPoint.Url)) { requestPath.Append(endPoint.Url); } else { requestPath.Append((this.EnableSsl && methodAttribute.IsSecure) ? this.SecureBaseUrl : this.BaseUrl); requestPath.Append(methodAttribute.MethodName); requestPath.Append(".xml"); } string boundary = string.Concat("-------------------------", DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture)); byte[] boundaryBytes = System.Text.Encoding.UTF8.GetBytes(string.Concat("\r\n--", boundary, "\r\n")); byte[] endBoundaryBytes = System.Text.Encoding.UTF8.GetBytes(string.Concat("\r\n--", boundary, "--\r\n")); List <byte> requestData = new List <byte>(); // If we have endpoint uploadtoken that is ALL we send and we don't send session/apikey // If we send session/apikey and NOT uploadtoken, Viddler will do a prepareupload behind the scenes and use that uploadtoken. // This is why allow_replace fails w/out an uploadtoken because their prepareupload does NOT have allow_replace set. if (endPoint != null && !string.IsNullOrEmpty(endPoint.Token)) { requestData.AddRange(boundaryBytes); requestData.AddRange(System.Text.Encoding.UTF8.GetBytes(string.Format(CultureInfo.InvariantCulture, "Content-Disposition: form-data; name=\"uploadtoken\"\r\n\r\n{0}", endPoint.Token))); } else { requestData.AddRange(boundaryBytes); requestData.AddRange(System.Text.Encoding.UTF8.GetBytes(string.Format(CultureInfo.InvariantCulture, "Content-Disposition: form-data; name=\"key\"\r\n\r\n{0}", this.ApiKey))); if (methodAttribute.IsSessionRequired) { requestData.AddRange(boundaryBytes); requestData.AddRange(System.Text.Encoding.UTF8.GetBytes(string.Format(CultureInfo.InvariantCulture, "Content-Disposition: form-data; name=\"sessionid\"\r\n\r\n{0}", this.SessionId))); } } if (parameters != null && parameters.Keys.Count > 0) { foreach (string key in parameters.Keys) { //string queryData = string.Format(CultureInfo.InvariantCulture, "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}", key, ViddlerHelper.EncodeRequestData(parameters[key])); string queryData = string.Format(CultureInfo.InvariantCulture, "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}", key, parameters[key]); requestData.AddRange(boundaryBytes); requestData.AddRange(System.Text.Encoding.UTF8.GetBytes(queryData)); } } long fileSize = endBoundaryBytes.Length; if (fileStream != null) { fileStream.Position = 0; if (string.IsNullOrEmpty(fileName)) { fileName = DateTime.Now.Ticks.ToString("x", CultureInfo.InvariantCulture); } requestData.AddRange(boundaryBytes); requestData.AddRange(System.Text.Encoding.UTF8.GetBytes(string.Format(CultureInfo.InvariantCulture, "Content-Disposition: form-data; name=\"file\"; filename=\"{0}\"\r\nContent-Type: application/octet-stream\r\n\r\n", fileName))); fileSize += fileStream.Length; } try { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestPath.ToString()); request.Method = "POST"; request.ContentType = string.Concat("multipart/form-data; boundary=", boundary); request.UserAgent = string.Concat("Microsoft .NET, ", this.GetType().Assembly.FullName); request.AllowWriteStreamBuffering = false; request.Accept = "text/xml"; request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; request.KeepAlive = true; request.Timeout = int.MaxValue; request.ReadWriteTimeout = int.MaxValue; request.ContentLength = requestData.Count + fileSize; Stream requestStream = request.GetRequestStream(); bool isCancel = false; try { requestStream.Write(requestData.ToArray(), 0, requestData.Count); requestStream.Flush(); if (fileStream != null) { byte[] buffer = new byte[32768]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { requestStream.Write(buffer, 0, bytesRead); requestStream.Flush(); if (this.Uploading != null) { ViddlerRequestUploadEventArgs uploadEventArgs = new ViddlerRequestUploadEventArgs(typeof(ContractType), fileStream.Length, fileStream.Position); this.Uploading(this, uploadEventArgs); if (uploadEventArgs.Cancel) { isCancel = true; request.Abort(); break; } } } } if (!isCancel) { requestStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); requestStream.Flush(); } } finally { if (!isCancel) { requestStream.Dispose(); } } if (isCancel) { return(default(DataType)); } else { DataType responseObject = ViddlerService.HandleHttpResponse <ContractType, DataType>(request, methodAttribute, this.DumpFolder); return(responseObject); } } catch (System.Net.WebException exception) { throw ViddlerService.HandleHttpError(exception); } }