首页 文章

在winforms应用程序中使用PHP webservice

提问于
浏览
3

目前我将C#mysql连接信息存储在类文件本身中,这看起来并不聪明,因为最终用户可以简单地使用像.NET Reflector这样的反射器来调试源代码,以防它没有被模糊 .

现在,stackoverflow上的用户建议创建一个将操纵数据库的Web服务 . 最终用户将使用的软件然后使用用户的凭据简单地使用Web服务对自身进行身份验证,然后使用它来访问资源 .

现在我有以下问题,我的服务器在linux ubuntu上运行,并且已经存储了一个使用plesk创建的网站 .

我知道我可以使用http://www.mono-project.com/在linux上托管web服务 . 但是我总是用PHP来做这些事情,而且我对如何将s#web-service上传到ssh服务器上安装的单声道版本感到困惑 .


我可以在winforms应用程序中使用类似下面的PHP代码作为C#Web服务吗?

PHP代码:

<?php
    class webService extends database
    {
        // Web service constructor
        public function __construct($username, $password, $uniqueId, $versionId)
        {
            $this->username = $username;
            $this->password = $password;
            $this->salt = 'xxx';
            $this->hash = 'xxx';
            $this->uniqueId = $uniqueid;
            $this->versionId = $versionId;
        }

        // Web service functions to check database values

        // Web service user account check function
        private function userCheck()
        {
            $this->connect();
            $userCheck = $this->execute_query("SELECT username, password FROM Users WHERE username = '" . $this->username . "' AND password = '" . $this->hash . "'");

            if($userCheck && mysqli_num_rows($userCheck) > 0)
            {
                return 'true';
            }
            else
            {
                return 'false';
            }
        }

        // Web service unique id check function
        private function uniqueCheck()
        {
            $this->connect();
            $uniqueCheck = $this->execute_query("SELECT username, uniqueid FROM Users WHERE username = '" . $this->username . "' AND uniqueid = '" . $this->uniqueId . "'");

            if($uniqueCheck && mysqli_num_rows($uniqueCheck) > 0)
            {
                return 'true';
            }
            else
            {
                return 'false';
            }
        }

        // Web service first run check function
        private function firstRunCheck()
        {
            $this->connect();
            $firstRunCheck = $this->execute_query("SELECT username, firstrun FROM Users WHERE username = '" . $this->username . "' AND firstrun = '0'");

            if($firstRunCheck && mysqli_num_rows($firstRunCheck) > 0)
            {
                return 'true';
            }
            else
            {
                return 'false';
            }
        }

        // Web service user disabled check function
        private function disabledUserCheck()
        {
            $this->connect();
            $disabledUserCheck = $this->execute_query("SELECT disabled FROM Users WHERE username = '" . $this->username . "' AND disabled = '1'");

            if($disabledUserCheck && mysqli_num_rows($disabledUserCheck) > 0)
            {
                return 'true';
            }
            else
            {
                return 'false';
            }
        }

        // Web service update required check function
        private function updateRequiredCheck()
        {
            $this->connect();
            $updateRequiredCheck = $this->execute_query("SELECT requiredupdate FROM requiredupdate WHERE version = '" . $this->versionId . "' AND requiredupdate = 1");

            if($updateRequiredCheck && mysqli_num_rows($updateRequiredCheck) > 0)
            {
                return 'true';
            }
            else
            {
                return 'false';
            }
        }

        // Web service premium check function
        private function userPremiumCheck()
        {
            $this->connect();
            $userPremiumCheck = $this->execute_query("SELECT premium FROM Users WHERE username = '" . $this->username . "' AND premium = 1");

            if($userPremiumCheck && mysqli_num_rows($userPremiumCheck) > 0)
            {
                return 'true';
            }
            else
            {
                return 'false';
            }
        }

        // Web service functions to update database values

        // Web service update first run parameters function
        private function firstRunUpdate()
        {
            $firstRunCheck = $this->firstRunCheck();

            if($firstRunCheck == 'true')
            {
                $this->connect();
                $this->execute_query("UPDATE Users SET uniqueid = '" . $this->uniqueId . "', firstrun = '1' WHERE username = '" . $this->username . "'");

                return 'true';
            }
            else
            {   
                return 'false';
            }
        }

        function to_xml(SimpleXMLElement $object, array $data)
        {   
            foreach ($data as $key => $value) {
                if (is_array($value)) {
                    $new_object = $object->addChild($key);
                    to_xml($new_object, $value);
                } else {   
                    $object->addChild($key, $value);
                }   
            }   
        }   

        // Web service handler function
        public function webService()
        {
            $userCheck = $this->userCheck();

            if($userCheck == 'true')
            {
                $userArray = array (
                    'username' => $this->username,
                    'authentificated' => $this->userCheck(),
                    'firstRun' => $this->firstRunCheck(),
                    'firstRunUpdated' => $this->firstRunUpdate(),
                    'uniqueIdCheck' => $this->uniqueCheck(),
                    'Premium' => $this->userPremiumCheck(),
                    'Disabled' => $this->disabledUserCheck(),
                    'updateRequired' => $this->updateRequiredCheck()
                );
            }
            else
            {
                $userArray = array (
                    'username' => $this->username,
                    'userCheck' => $this->userCheck()
                );
            }

            echo str_replace("\/", "/", json_encode($userArray, JSON_PRETTY_PRINT));
        }
    }
?>

或者如何创建可以在我的应用程序中使用的PHP Web服务?


PHP脚本的当前响应如下所示:

{ "username": "dane", "authentificated": "true", "firstRun": "false", "firstRunUpdated": "false", "uniqueIdCheck": "true", "Premium": "true", "Disabled": "false", "updateRequired": "false" }

2 回答

  • 1

    RômuloM . Farias的回答很明显 .

    但是,您可能需要更多解释,第一次“手动”做一些事情可能会有所帮助,这样当您使用框架为您忙碌工作时,您可以了解“幕后”的内容 . 所以我放弃了答案:

    ...

    看起来你正处于正确的轨道上,你想要做的事情的基本结构(MySQL凭据和访问是在服务器端处理的) .

    您的服务器上没有任何理由需要运行C#,因为您的客户端应用程序是WinForms . Web服务的重点是允许不同的平台轻松地相互通信 .

    Web服务可以被认为是返回数据而不是HTML的网站 . (实际上它是相反的:网站或网页只是一种返回html的特定网络服务)

    因此,您现在需要的是让您的WinFroms应用程序能够通过Web服务与您在上面发布的PHP代码进行通信 . 换句话说,我们希望将您在Web服务中获得的PHP代码包装起来,然后让您的WinForms应用程序使用Web服务 .

    请记住,为了安全起见,您必须确保使用SSL并执行POST,而不是GET(即密码必须在邮件正文中加密,而不是粘贴在URL上!)

    以下是一些有关创建PHP Web服务的教程(必须存在更好的内容):https://davidwalsh.name/web-service-php-mysql-xml-json https://www.codeproject.com/Tips/671437/Creating-Web-Service-Using-PHP-Within-Minutes

    有很多方法可以从C#中使用Web服务 . RestSharp可能是理想的 .

    请注意,您可能会选择SOAP或JSON作为格式 . 因此,请查找使用相同格式的Web服务教程和使用者教程 . (构建一个可以返回不同格式的Web服务,具体取决于客户端请求的理想情况,但更高级 . 我在PHP中没有相关的教程)

    (在此上下文中,“API”与“Web服务”同义,但请注意“API”也可以具有完全不同的含义)

    你可能看起来还有很长的路要走,但不要担心,一旦你的第一个例子工作,使用相同的方法来做各种好东西都会很快捷 .

  • 3

    使用API时,数据访问层将与客户端软件完全分离 . Web服务只需要以特定的方式打印响应,以便客户理解(XML,JSON,YAML等) .

    在C#应用程序中,您将调用该API,然后将响应转换为C#对象 . 在C#中我通常使用RestSharp . 它易于使用,并可以从XML或JSON转换为C#对象 .

    在您的PHP服务器中,考虑使用一些处理HTTP代码和响应的框架或微框架 . 服务器是否有责任向客户端发送正确的响应(响应内容类型,状态等) . 尝试用手工做有时候会很无聊 . 你的代码现在没有任何问题,但很快就会变成一个小怪物 .

    对于您的情况LumenSilex可以简单而有用!

相关问题