Exemplo n.º 1
0
        //Uploading the generated file to FTP server
        public static void UploadFileToFtp(string BAI2)
        {
            try
            {
                var fileName = "";
                Console.WriteLine("---Uploading File---");
                //Path where test file stored
                string sourcePath = dirPath + @"\Resources\FTPFileGenerated\";
                //Invalid file to upload
                if (BAI2 != "Invalid")
                {
                    fileName = filename + ".txt";
                }
                //Valid file to upload
                else
                {
                    fileName = filename;
                }

                // Use Path class to manipulate file and directory paths.
                string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
                destFile = System.IO.Path.Combine(FTPPath, fileName);

                //FileStream for holding the file
                FileStream fStream = new FileStream(destFile, FileMode.Create);

                //connect to the server
                FileWebRequest fileRequest = (FileWebRequest)FtpWebRequest.Create(new Uri(sourceFile));

                //set the protocol for the request
                fileRequest.Method = WebRequestMethods.Ftp.DownloadFile;

                string username   = WebUtilities.fetchParamValFromConfig("username");
                string password   = WebUtilities.fetchParamValFromConfig("password");
                string dePassword = WebUtilities.decryptString(password);

                //provide username and password
                fileRequest.Credentials = new NetworkCredential(username, dePassword);

                //get the servers response
                WebResponse response = fileRequest.GetResponse();

                //retrieve the response stream
                Stream stream = response.GetResponseStream();

                //create byte buffer
                byte[] buffer = new byte[1024];
                long   size   = 0;

                //determine how much has been read
                int totalRead = stream.Read(buffer, 0, buffer.Length);

                //loop through the total size of the file
                while (totalRead > 0)
                {
                    size += totalRead;

                    //write to the stream
                    fStream.Write(buffer, 0, totalRead);

                    //get remaining size
                    totalRead = stream.Read(buffer, 0, 1024);
                }
                // Close the streams.
                fStream.Close();
                stream.Close();
                fileExistsInFTPflag = true;
            }
            catch (Exception e)
            {
                Assert.Fail("File is not successfully uploaded into FTP folder location: " + e);
            }
        }
Exemplo n.º 2
0
        //This is for sending a post request
        private static void post(string jsonData)
        {
            try
            {
                // Create a request using a URL that can receive a post.
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strAPIUrl);
                // Set the Method property of the request to POST.
                request.Method = "POST";
                // Create POST data and convert it to a byte array.
                byte[] byteArray = Encoding.UTF8.GetBytes(jsonData);
                // Set the ContentType property of the WebRequest.
                request.ContentType = "application/json";
                // Set the ContentLength property of the WebRequest.
                request.ContentLength = byteArray.Length;

                string          username   = WebUtilities.fetchParamValFromConfig("username");
                string          password   = WebUtilities.fetchParamValFromConfig("password");
                string          dePassword = WebUtilities.decryptString(password);
                CredentialCache credcache  = new CredentialCache();
                credcache.Add(new Uri(strAPIUrl), "NTLM", new NetworkCredential(username, dePassword));
                request.Credentials = credcache;

                // Get the request stream.
                dataStream = request.GetRequestStream();
                // Write the data to the request stream.
                dataStream.Write(byteArray, 0, byteArray.Length);
                // Close the Stream object.
                dataStream.Close();
                // Get the response.
                response = (HttpWebResponse)request.GetResponse();
                // Display the status.
                Console.WriteLine(((HttpWebResponse)response).StatusDescription);
                statuscode = ((HttpWebResponse)response).StatusCode;
                statusCode = (int)statuscode;

                // Get the stream containing content returned by the server.
                dataStream = response.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader reader = new StreamReader(dataStream);
                // Read the content.
                string responseFromServer = reader.ReadToEnd();
                // Display the content.
                Console.WriteLine(responseFromServer);
                // Clean up the streams.
                reader.Close();
                dataStream.Close();
                response.Close();

                jsonResArray = JArray.Parse(responseFromServer);
            }
            catch (WebException webEx)
            {
                try
                {
                    if (webEx.Response != null)
                    {
                        response   = (HttpWebResponse)webEx.Response;
                        statuscode = ((HttpWebResponse)response).StatusCode;
                        statusCode = (int)statuscode;

                        // Get the stream containing content returned by the server.
                        dataStream = response.GetResponseStream();
                        // Open the stream using a StreamReader for easy access.
                        StreamReader reader = new StreamReader(dataStream);
                        // Read the content.
                        string responseFromServer = reader.ReadToEnd();
                        // Display the content.
                        Console.WriteLine(responseFromServer);
                        // Clean up the streams.
                        reader.Close();
                        dataStream.Close();
                        response.Close();

                        if (statusCode == 200 || statusCode == 400)
                        {
                            jsonResArray = JArray.Parse(responseFromServer);
                        }
                        else
                        {
                            jsonResObject = JObject.Parse(responseFromServer);
                        }
                    }
                }catch (Exception e)
                {
                    Assert.Fail("Failed to send the Post request" + e);
                }
            }
        }