Click here to Skip to main content
14,668,844 members
Rate this:
Please Sign up or sign in to vote.
See more:
Dear All,

I'm trying to create windows service to upload data from the client machine to the server using Ajax/web-server to SQL database.

Here, the window's services will create one log file.

The file on the client's machine is encrypted, so the windows services will first decrypt and then it will upload data onto the server. And there is one communication helper to upload data from the client's machine to the server.

The services are successfully getting the file and decrypting, but not uploading the data onto the server.

What I have tried:

public string UploadOfflineResult()
            var pathSouce = Directory.GetParent(Directory.GetParent(Environment.SystemDirectory.ToString()).ToString()).FullName;
            var offlinePath = pathSouce + ConfigurationManager.AppSettings["resultFile"];
            FileInfo _file = new FileInfo(offlinePath);
            WriteToFile(_file.Exists ? "Offline result file exist" : "Offline result file does not exist");

            if (_file.Exists && getPing())
                WriteToFile("Working ON offline exam result file");
                OfflineExamResult offlineExamResult = null;
                    FileStream fs = new FileStream(@"" + offlinePath, FileMode.Open);
                    WriteToFile("converted to stream file: " + @"" + offlinePath);
                    using (StreamReader sr = new StreamReader(fs))
                        WriteToFile("Reading file");
                        string data = sr.ReadToEnd();
                        offlineExamResult = Cryptography.Decrypt<OfflineExamResult>(data);
                        WriteToFile("CS: " + offlineExamResult.CandidateSessionID.ToString());
                        WriteToFile("Offline result file DECRYPTED");

                        //Not able to Upload data CommunicationHelper.UpdateCandidateOfflineExamResultUsingWindowService(
                            JsonConvert.SerializeObject(offlineExamResult, Formatting.None, new JsonSerializerSettings
                                NullValueHandling = NullValueHandling.Ignore

                        WriteToFile("UpdatedOfflineCandidateExamResult: CandidateSessionID=" + offlineExamResult.CandidateSessionID + " ");
                        return true.ToString();
                catch (Exception ex)
                    ex = ex.InnerException; //Avoid warning
                    return JsonConvert.SerializeObject(
                        new { success = false, message = "Invalid result file content" },
                        new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });

            return false.ToString();

public static string UpdateCandidateOfflineExamResultUsingWindowService(string _offlineExamResult, string fullUrl)
           WebClient webClient = new WebClient();
           webClient.Encoding = Encoding.UTF8;

           string postData = string.Format("offlineExamResult={0}", _offlineExamResult);
           string result = Task.Run(() => webClient.UploadStringTaskAsync(fullUrl, postData)).Result;

           return true.ToString();

Can anyone please help me.

Updated 10-Jun-19 2:40am
F-ES Sitecore 9-Jun-19 8:14am
Where is the code that does the upload?
Dave Kreskowiak 9-Jun-19 10:56am
You never showed the code for this call:

And you never explained what happens when this call is made or any error messages returned.

It's impossible for anyone to help you without that information.
abdul subhan mohammed 9-Jun-19 13:23pm
To convert into the offlineResultInfo.cs
Dave Kreskowiak 9-Jun-19 13:40pm
Your statement is meaningless. I have no idea what "offliceResultInfo.cs" is nor do I have any access to it because it's on your machine.
abdul subhan mohammed 9-Jun-19 13:25pm
Got no errors and nothing happening. I checked the log file, its working till 'File Decrypted' but its not able to communicate the web server.
Dave Kreskowiak 9-Jun-19 13:40pm
Without the code your wrote for CommunicationHelper.UpdateCandidateOfflineExamResultUsingWindowService, it's impossible to tell you anything useful.
Gerry Schmitz 9-Jun-19 12:12pm
And why do you need to "decrypt" before uploading. Makes no sense.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You're not sending json formatted data to your webservice. You're sending "something={jsondata}". You don't do that. You send just the json formatted data, not the variable= part.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100