private static string UploadToDPSR(FileInfo fi, string URI)
        {
            string fileName = fi.Name;

            byte[]    fileContents = File.ReadAllBytes(fi.FullName);
            const int tentatives   = 5;
            string    res          = "Upload process failed";

            for (int i = 0; i < tentatives; i++)
            {
                var webService     = new Uri(@URI);
                var requestMessage = new HttpRequestMessage(HttpMethod.Post, webService);
                requestMessage.Headers.ExpectContinue = false;

                var multiPartContent = new MultipartFormDataContent("----MyGreatBoundary");
                var byteArrayContent = new ByteArrayContent(fileContents);
                byteArrayContent.Headers.Add("Content-Type", "application/octet-stream");
                multiPartContent.Add(byteArrayContent, "file", fileName);
                //multiPartContent.Add(new StringContent("generator=ei"), "gen", "ei");
                requestMessage.Content = multiPartContent;

                var httpClient = new HttpClient();
                try
                {
                    Task <HttpResponseMessage> httpRequest  = httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
                    HttpResponseMessage        httpResponse = httpRequest.Result;
                    HttpStatusCode             statusCode   = httpResponse.StatusCode;
                    HttpContent responseContent             = httpResponse.Content;

                    if (responseContent != null)
                    {
                        Task <string>          stringContentsTask = responseContent.ReadAsStringAsync();
                        string                 stringContents     = stringContentsTask.Result;
                        int                    first      = stringContents.IndexOf('{');
                        int                    length     = stringContents.LastIndexOf('}') - first + 1;
                        string                 JSONFormat = stringContents.Substring(first, length);
                        DPSReportsResponseItem item       = JsonConvert.DeserializeObject <DPSReportsResponseItem>(JSONFormat, new JsonSerializerSettings
                        {
                            ContractResolver = new DefaultContractResolver()
                            {
                                NamingStrategy = new CamelCaseNamingStrategy()
                            }
                        });
                        return(item.Permalink);
                    }
                }
                catch (Exception e)
                {
                    res = e.GetFinalException().Message;
                }
                finally
                {
                    byteArrayContent.Dispose();
                    httpClient.Dispose();
                    requestMessage.Dispose();
                }
            }
            return(res);
        }
示例#2
0
        private string UploadToDPSR(FileInfo fi, string URI)
        {
            string fileName = fi.Name;

            byte[]             fileContents   = File.ReadAllBytes(fi.FullName);
            Uri                webService     = new Uri(@URI);
            HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, webService);

            requestMessage.Headers.ExpectContinue = false;

            MultipartFormDataContent multiPartContent = new MultipartFormDataContent("----MyGreatBoundary");
            ByteArrayContent         byteArrayContent = new ByteArrayContent(fileContents);

            byteArrayContent.Headers.Add("Content-Type", "application/octet-stream");
            multiPartContent.Add(byteArrayContent, "file", fileName);
            //multiPartContent.Add(new StringContent("generator=ei"), "gen", "ei");
            requestMessage.Content = multiPartContent;

            HttpClient httpClient = new HttpClient();

            try
            {
                Task <HttpResponseMessage> httpRequest  = httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
                HttpResponseMessage        httpResponse = httpRequest.Result;
                HttpStatusCode             statusCode   = httpResponse.StatusCode;
                HttpContent responseContent             = httpResponse.Content;

                if (responseContent != null)
                {
                    Task <String>          stringContentsTask = responseContent.ReadAsStringAsync();
                    String                 stringContents     = stringContentsTask.Result;
                    int                    first      = stringContents.IndexOf('{');
                    int                    length     = stringContents.LastIndexOf('}') - first + 1;
                    String                 JSONFormat = stringContents.Substring(first, length);
                    DPSReportsResponseItem item       = JsonConvert.DeserializeObject <DPSReportsResponseItem>(JSONFormat);
                    String                 logLink    = item.permalink;
                    return(logLink);
                }
            }
            catch (Exception ex)
            {
                return(ex.Message);
                // Console.WriteLine(ex.Message);
            }
            return("");
        }
        private static string UploadToDPSR(FileInfo fi, string URI, OperationController operation)
        {
            string fileName = fi.Name;

            byte[]    fileContents = File.ReadAllBytes(fi.FullName);
            const int tentatives   = 5;
            string    res          = "Upload process failed";

            for (int i = 0; i < tentatives; i++)
            {
                operation.UpdateProgressWithCancellationCheck("Upload tentative");
                var webService     = new Uri(@URI);
                var requestMessage = new HttpRequestMessage(HttpMethod.Post, webService);
                requestMessage.Headers.ExpectContinue = false;

                var multiPartContent = new MultipartFormDataContent("----MyGreatBoundary");
                var byteArrayContent = new ByteArrayContent(fileContents);
                byteArrayContent.Headers.Add("Content-Type", "application/octet-stream");
                multiPartContent.Add(byteArrayContent, "file", fileName);
                //multiPartContent.Add(new StringContent("generator=ei"), "gen", "ei");
                requestMessage.Content = multiPartContent;

                var httpClient = new HttpClient();
                try
                {
                    Task <HttpResponseMessage> httpRequest  = httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None);
                    HttpResponseMessage        httpResponse = httpRequest.Result;
                    HttpStatusCode             statusCode   = httpResponse.StatusCode;
                    HttpContent responseContent             = httpResponse.Content;

                    if (statusCode != HttpStatusCode.OK)
                    {
                        throw new HttpRequestException(statusCode.ToString());
                    }

                    if (responseContent != null)
                    {
                        Task <string>          stringContentsTask = responseContent.ReadAsStringAsync();
                        string                 stringContents     = stringContentsTask.Result;
                        DPSReportsResponseItem item = JsonConvert.DeserializeObject <DPSReportsResponseItem>(stringContents, new JsonSerializerSettings
                        {
                            ContractResolver = new DefaultContractResolver()
                            {
                                NamingStrategy = new CamelCaseNamingStrategy()
                            }
                        });
                        if (item.Error != null)
                        {
                            throw new InvalidOperationException(item.Error);
                        }
                        operation.UpdateProgressWithCancellationCheck("Upload tentative successful");
                        return(item.Permalink);
                    }
                }
                catch (Exception e)
                {
                    res = e.GetFinalException().Message;
                    operation.UpdateProgressWithCancellationCheck("Upload tentative failed: " + res);
                }
                finally
                {
                    byteArrayContent.Dispose();
                    httpClient.Dispose();
                    requestMessage.Dispose();
                }
            }
            return(res);
        }