开发者

C# WebRequest to php script carry out insert statements problems

Okay basically

 /// <summary>
        /// Sends the process.
        /// </summary>
        /// <param name="MD5">The M d5.</param>
        /// <param name="procName">Name of the proc.</param>
        /// <param name="procLoc">The proc loc.</param>
        public void SendProcess(string MD5, string procName, string procLoc)
        {
            // Create a request using a URL that can receive a post. 
            WebRequest request = WebRequest.Create("http://localhost/EDAC//SubmitProc.php ");
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            string postData = "MD5=" + MD5 + "&procName=" + procName + "&procLoc=" + procLoc + "&userID=" + _userID;
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
        }

This function is called in a loop

Process[] currentProcess = Process.GetProcesses();

                foreach (var process in currentProcess)
                {
                    var isContained = false;

                    foreach (var heldProcess in _processlist)
                    {
                        if (heldProcess.Id == process.Id)
                        {
                            isContained = true;
                        }
                    }
                    if (!(isContained))
                    {

                        try
                        {
                                _processLocs.Add(process.MainModule.FileName);
                                _processlist.Add(process);
                                _tw.WriteLine(process.ProcessName);
                                _tw.WriteLine(process.MainModule.FileName);
                                var md5 = GetMD5HashFromFile(process.MainModule.FileName);
                                _tw.WriteLine(md5);
                                SendProcess(md5, process.ProcessName, process.MainModule.FileName);
                        }
                        catch (Win32Exception ex)
                        {

                        }

                    }
                }

Basically this method开发者_Python百科 is fine on my local host just not on web server, now this method is obviously not idle considering when program first loads you get about 30 connection requests and messages sent within half a second, it locks up basically, probably due to not getting a connection.

So whats the right way of doing this? Ive been looking for a way to get a loop going that waits until server is ready to take more data or is it possibly sending all of the objects and handling that into variables for insert php side


Okay basically

public void SendProcess(string md5, string procName, string procLoc)
{
    var values = new NameValueCollection
                 {
                     { "MD5",      md5      },
                     { "procName", procName },
                     { "procLoc",  procLoc  },
                     { "userID",   _userID  },
                 };

    using (var client = new WebClient())
    {
        client.UploadValues("http://localhost/EDAC/SubmitProc.php", values);
    }
}

You can call this function in a loop, and it should work.

Note how the resources used (here: the WebClient) are disposed, so subsequent calls can reuse the same HTTP connection (connection pooling). Otherwise you quickly run into lots of open connections that cannot be reused.

Now, repeatedly making lots of small HTTP requests is much less efficient than making a few larger requests. You should merge multiple requests into a single one and limit the number of requests per time unit to a reasonable level. You can use WebClient.UploadString or WebClient.UploadData to upload more complex data than simple key/value-pairs.

That said, I'd never want to have a piece of software on my machine that sends the list of active processes to some server. I hope you have a big, fat warning in your application that warns users about your intrusion of their privacy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜