Ejemplo n.º 1
0
        public bool UploadSingleJson()
        {
            string jsonData = SessionHelper.Read <string>("jsonData");

            try
            {
                // Login to the remote server
                HttpWebRequest httpRequest = null;
                string         uriString   = sisOnlineURL + @"Defws/Login?UserId=" + SessionHelper.LoginInfo.LoginID + "&pwrd=" + SessionHelper.LoginInfo.Password;
                Debug.WriteLine("UploadSingleJson uriString: " + uriString);
                // string encodedUrl = WebUtility.UrlEncode(uriString);

                Uri sisServerUri = new Uri(uriString);
                // Debug.WriteLine("UploadSingleJson uriString: " + sisServerUri.ToString());
                Debug.WriteLine("UploadSingleJson  sisServerUri: " + sisServerUri.ToString());

                httpRequest = (HttpWebRequest)WebRequest.Create(sisServerUri);
                CookieContainer cc = new CookieContainer();
                httpRequest.CookieContainer = cc;
                httpRequest.Method          = WebRequestMethods.Http.Get;

                // Get back the HTTP response from remote server for the login
                using (HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse())
                {
                    using (Stream httpResponseStream = httpResponse.GetResponseStream())
                    {
                        string response = String.Empty;
                        using (StreamReader reader = new StreamReader(httpResponseStream))
                        {
                            response = reader.ReadToEnd();
                        }

                        Debug.WriteLine("Response: " + response);
                    }
                }

                // *** RRB 10/28/15 - There should be a check here for a valid login response.
                // The login logic above should be in its own method and used by all the logins.

                // Transmit the Assessment to the remote server.
                using (var client = new CookieWebClient(cc))
                {
                    var data = new NameValueCollection()
                    {
                        { "json", jsonData }
                    };
                    byte[] result = client.UploadValues(sisOnlineURL + "Defws/" + "UpdateAssessmentJSONVenture", "POST", data);

                    Debug.WriteLine("Status code: " + client.StatusCode());
                    if (result.Length == 0)
                    {
                        return(true);
                    }
                    if (result == null)
                    {
                        Debug.WriteLine("Upload JSON error  result is null.");
                    }
                    if (result.Length > 0)
                    {
                        Debug.WriteLine("Upload JSON error  result.length: " + result.Length);
                    }

                    Debug.WriteLine("Upload JSON error: " + Encoding.ASCII.GetString(result));

                    return(false);
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine("UploadSingleJSON:" + ex.Message);
                if (ex.InnerException != null && ex.InnerException.Message != null)
                {
                    Debug.WriteLine("UploadSingleJSON InnerException: " + ex.InnerException.Message);
                }

                return(false);
            }
        }