如何使用IP地址查找电脑或系统?

时间:2017-12-26 10:32:10

标签: c# asp.net winforms web-services wcf

我有一个Web应用程序和一个桌面应用程序,两者都是使用服务连接的。有一个功能是从运行桌面应用程序的桌面的Web应用程序中截取屏幕截图。 我已经编写了代码来截取屏幕截图。 我唯一坚持的是如何将请求仅发送给特定用户?

现在我发送一个带有用户ID的请求,它会搜索所有正在使用该应用程序的用户,如果用户ID匹配,则会截取屏幕截图。

这是我通过所有用户进行循环的代码,因为当用户群增长时会增加额外的负担

private void StartHttpCommandDispatcher()
        {
            mCmdDispatcher.AddResourceLocator(new ImageLocator(SMARTer.Properties.Resources.ResourceManager));
            mCmdDispatcher.AddCommand(new SpeakTextCommand());
            mCmdDispatcher.RequestReceived += new HttpCommandDispatcher.RequestReceivedHandler(OnHttpRequestReceived);
            mCmdDispatcher.Start(SCREEN_CAPTARE_URL);
        }
        private void ShowHttpRequest(string request)
        {
            NameValueCollection queryCollection = SplitNameValuePairs(request);
            string[] deatils = GetTextParameter(queryCollection);
            if (deatils != null)
            {
                _rquestedEmpUserId = Convert.ToInt64(deatils[0]);
                int CurrentEmpUserId = Client.DataService.GetEmpUserId(System.Environment.UserName);
                if (CurrentEmpUserId == _rquestedEmpUserId)
                {
                    onDemandScreenShot(Convert.ToInt64(deatils[1]), new Guid(deatils[2]));

                }
            }
        }

有没有办法如果我发送特定系统的IP地址,那么它会请求同一系统而不是循环遍历所有系统?

*****来自桌面应用程序的代码******

public void Start(string url)
    {
        if (!HttpListener.IsSupported)
        {
            MessageBox.Show("Windows XP SP2 or Server 2003 is required.", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
        try
        {
            mListener = new HttpListener();
            // Add the prefix.
            mListener.Prefixes.Add(url);
            mListener.Start();
            mListener.BeginGetContext(new AsyncCallback(this.ProcessRequest), null);
        }
        catch (Exception e)
        {
            MessageBox.Show(string.Format("Unable to start the web server.\n\n{0}", e.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    public void End()
    {
        if (mListener != null)
        {
            HttpListener listener = mListener;
            mListener = null;
            listener.Close();
        }
    }
    public void AddCommand(HttpCommand command)
    {
        Debug.Assert(!mCommands.ContainsKey(command.Name));
        mCommands[command.Name] = command;
    }

    public void AddResourceLocator(ResourceLocator locator)
    {
        foreach (string extension in locator.Extensions)
        {
            Debug.Assert(!mResourceLocators.ContainsKey(extension));
            mResourceLocators[extension] = locator;
        }
    }

    public void ProcessRequest(IAsyncResult result)
    {
        if (mListener == null)
        {
            return; // Listener is has  been closed
        }
        // Call EndGetContext to complete the asynchronous operation.
        HttpListenerContext context = mListener.EndGetContext(result);
        try
        {
            DispatchCommand(context);
        }
        catch (Exception e)
        {
            MessageBox.Show(string.Format("Command failed.\n\n{0}", e.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            mListener.BeginGetContext(new AsyncCallback(this.ProcessRequest), null);
        }
    }

    private void DispatchCommand(HttpListenerContext context)
    {
        HttpListenerRequest request = context.Request;

        // Inform listeners that a request has been received.
        // Only send up to 240 characters of the URL
        const int MaxNotifyRequestLength = 240;
        string unescapedUrl = request.RawUrl;
        string notifyRequest = unescapedUrl.Substring(0, Math.Min(MaxNotifyRequestLength, unescapedUrl.Length));
        if (request.RawUrl.Length > notifyRequest.Length)
        {
            notifyRequest += "..."; // Indicate the URL has been truncated.
        }
        OnRequestReceived(notifyRequest);

        string queryString = string.Empty;
        int urlEndPos = request.RawUrl.IndexOf('?');
        if (request.HttpMethod == "GET")
        {
            if (urlEndPos != -1 && urlEndPos < request.RawUrl.Length - 1)
            {
                queryString = request.RawUrl.Substring(urlEndPos + 1);
            }
        }
        else
        {
            queryString = GetQueryStringFromPostData(request);
        }

        // Split query string args into name/value pairs
        NameValueCollection queryCollection = SplitNameValuePairs(queryString);

        HttpListenerResponse response = context.Response;
        response.ContentType = "text/html";
        response.ContentLength64 = 0;
        System.IO.Stream output = response.OutputStream;

        // Extract a lowercase URL that doesn't include the query string.
        string url = urlEndPos != -1 ? request.RawUrl.Substring(0, urlEndPos) : request.RawUrl;
        url = url.Trim(new char[] { '/' });
        url = url.ToLower();

        // If it's a request for our dummy gif, send the dummy
        // gif image as a response and then process the actual command
        if (url.EndsWith("/dummy.gif"))
        {
            // Remove /dummy.gif from the url
            url = url.Substring(0, url.Length - 10);

            // Send the gif image
            response.ContentType = "img/gif";
            response.ContentLength64 = mDummyGif.Length;
            output.Write(mDummyGif, 0, mDummyGif.Length);
        }

        // If the url has an extension (e.g. .png) then the
        // request is for a file (e.g. an image), so see if
        // a ResourceLocator is registered to handle this extension.
        string extension = Path.GetExtension(url);
        bool foundResource = false;
        if (!string.IsNullOrEmpty(extension))
        {
            extension = extension.Substring(1);
            ResourceLocator locator;
            if (mResourceLocators.TryGetValue(extension, out locator))
            {
                locator.OutputResource(Path.GetFileNameWithoutExtension(url), extension, context);
                foundResource = true;
            }
        }
        // Not a request for a resource, must be a command
        if (!foundResource)
        {
            HttpCommand command;
            if (mCommands.TryGetValue("SpeakText", out command))
            {
                command.Execute(queryCollection, context);
            }
        }
        // You must close the output stream.
        output.Close();
    }

这是Web应用程序上的Capture Link

<a href='javascript:var server="localhost:60024/SMARTer";var maxreqlength=1500;var EmpUserID="68675";if(EmpUserID){_bufferText(EmpUserID);_speakText()}void 0;function _formatCommand(b,a){return"http://"+server+"/"+b+"/dummy.gif"+a+"&timestamp="+new Date().getTime()}function _speakText(){var a=new Image(1,1);a.onerror=function(){_showerror()};a.src=_formatCommand("speaktext","?source="+document.URL)}function _bufferText(f){var c="true";var b=Math.floor((f.length+maxreqlength-1)/maxreqlength);for(var d=0;d<b;d++){var g=d*maxreqlength;var a=Math.min(f.length,g+maxreqlength);var e=new Image(1,1);e.onerror=function(){_showerror()};e.src=_formatCommand("buffertext","?totalreqs="+b+"&req="+(d+1)+"&EmpUserID="+f.substring(g,a)+"&clear="+c);c="false"}}function _showerror(){alert("Server is not running.")}'>Capture</a>

正如您所看到的,我们在链接中提到了EmpUserID,我们想要截取屏幕截图。

在桌面应用程序的web.config文件中,我们添加一个键来监听该链接:

<add key="ScreenCaptureUrl" value="http://localhost:60024/SMARTer/" />

0 个答案:

没有答案