CodeGo.net>在WinForms应用程序中使用PHP Web服务
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了CodeGo.net>在WinForms应用程序中使用PHP Web服务,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5899字,纯文字阅读大概需要9分钟。
内容图文
当前,我将C#mysql连接信息存储在类文件本身中,这似乎并不聪明,因为最终用户可以简单地使用NET Reflector之类的反射器来调试源代码,以防万一.
现在,使用stackoverflow的用户推荐创建一个将操作数据库的Web服务.最终用户将使用的软件然后使用用户的凭据通过Web服务简单地对自身进行身份验证,然后使用它来访问资源.
现在,我遇到以下问题,我的服务器正在linux ubuntu上运行,并且已经存储了使用plesk创建的网站.
我知道我可以使用http://www.mono-project.com/在Linux上托管Web服务.但是我从来没有做过,因为我一直使用PHP来做这样的事情,而且我对如何将C#Web服务上载到ssh服务器上已安装的mono版本感到困惑.
我可以在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" }
解决方法:
RômuloM. Farias的答案是正确的.
但是,您可能需要更多的解释,这可能有助于第一次“手动”执行某些操作,因此您可以了解使用框架为您忙碌的工作时“幕后”的情况.所以我让我的答案站起来:
…
看起来您在正确的位置上了要执行的操作的基本结构(MySQL凭据和访问权在服务器端处理).
没有理由只因为客户端应用程序WinForms在服务器上运行任何C#. Web服务的重点是允许不同的平台轻松地相互通信.
可以将Web服务视为返回数据而不是HTML的网站. (实际上是相反的:网站或网页只是返回html的一种特定类型的Web服务)
因此,您现在所需要的就是让您的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”也可以具有完全不同的含义)
看起来您还有很长的路要走,但是不用担心,一旦您有了第一个示例,就可以快速轻松地使用相同的方法来完成各种出色的工作.
内容总结
以上是互联网集市为您收集整理的CodeGo.net>在WinForms应用程序中使用PHP Web服务全部内容,希望文章能够帮你解决CodeGo.net>在WinForms应用程序中使用PHP Web服务所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。