Example #1
0
        /// <summary>
        ///     Web service test event handler
        /// </summary>
        /// <param name="sender"> </param>
        /// <param name="e"> </param>
        private void RecpConnTestConnection_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor = Cursors.WaitCursor;
                ValidateWebService();

                var credentials = new StringDictionary
                                      {
                                          {"user", RecpConnUserName.Text},
                                          {"password", RecpConnPassword.Text}
                                      };
                var vsSyncConfig = Config.Instance;
                var outFolder = (rbMei.Checked) ? MeiOutputPath.Text : VmOutputFolderPath.Text;
                var testFile = Path.Combine(outFolder, vsSyncConfig.ConnectionFileName);
                if (!File.Exists(testFile))
                    File.Copy(Path.Combine(Application.StartupPath, vsSyncConfig.ConnectionFileName), testFile);

                var dataFiles = new UploadSpec[] { new UploadSpec(vsSyncConfig, "configtest", vsSyncConfig.ConnectionFileName) };
                //var dataFiles = new[]
                //                    {
                //                        new UploadSpec(default(byte[]), CommandType.DefProductsCommand.Name, null),
                //                        new UploadSpec(default(byte[]), CommandType.DefInventoryCommand.Name, null)
                //                    };

                var response = HttpUpload.Upload(new Uri(WebServiceUrl.Text), credentials,null, dataFiles);
                var stm = response.GetResponseStream();
                CheckResponceStream(stm);
                MessageBox.Show(
                    response.StatusCode != HttpStatusCode.OK
                        ? "Test failed. Server response status is {0}".F(response.StatusDescription)
                        : @"Test Passed",
                    @"Connection Test");
            }
            catch (WebException ex)
            {
                var str = ex.Response != null
                              ? new StreamReader(ex.Response.GetResponseStream()).ReadToEnd()
                              : "Invalid server response.";
                if (
                    str.Contains("Please include at least one file."))
                {
                    MessageBox.Show(@"Test Passed", @"Connection Test");
                    return;
                }
                MessageBox.Show(@"Test Failed.{2}{0}{2}{1}".F(ex.Message, str, Environment.NewLine), @"Connection Test",
                                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally
            {
                Cursor = Cursors.Default;
            }
        }
        private string SendPostRequest(UploadSpec[] dataFiles)
        {
            string str;
            try
            {

                var netCredentials = default(ICredentials);//new NetworkCredential(credentials["user"], credentials["password"]);
               // var netCredentials = new NetworkCredential(credentials["user"], credentials["password"]);
                return HttpUpload.Upload(uri, credentials, netCredentials,  dataFiles).AsString();
            }
            catch (WebException ex)
            {
                str = ex.Message +" " + (ex.Response != null
                          ? ex.Response.AsString()
                          : "Invalid server response.");
            }
            return str;
        }
        private void SendingToWebService()
        {
            Logger.Info("Calling web service...");
            var config = Configuration.Config.Instance;
            var servCfg = config.WebService;
            string dir = config.GetOutputFolder();

              var parameters = new StringDictionary();
              parameters.Add("user", servCfg.Login);
              parameters.Add("password", Decrypt(servCfg.Password));

            var uploads = new List<UploadSpec>();
            var commands = Config.Task.Commands;
            foreach (var commandType in commands)
            {
                if (commandType.Enabled)
                {
                    var products = new UploadSpec(config, commandType.Name, servCfg.DoArchiving ? GetArchName(commandType.Name) : commandType.OutputFileName);
                    uploads.Add(products);
                }
            }
            if (uploads.Count == 0) return;
            var dataFiles = uploads.ToArray();

            try
            {
                using (var response = HttpUpload.Upload(new Uri(servCfg.Url), parameters, null, dataFiles))
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        Logger.Error("Error sending file. " + response.StatusDescription);
                        throw new TaskException(@"Sending is failed. The Server responce status is '{0} - {1}'.".F(response.StatusCode, response.StatusDescription), this);
                    }
                    else
                    {
                        UpdateLastSentDate(dir);
                    }
                }
            }
            catch (WebException we)
            {
                var str = we.Message + " " + (we.Response != null
                  ? new StreamReader(we.Response.GetResponseStream()).ReadToEnd()
                  : "Invalid server response.");

                Logger.Error(str, we);
                throw we;
            }
        }