[英]Using PHP webservice in winforms application
目前我將C#mysql連接信息存儲在類文件本身中,這看起來並不聰明,因為最終用戶可以簡單地使用像.NET Reflector這樣的反射器來調試源代碼,以防它沒有被模糊。
現在,stackoverflow上的用戶建議創建一個將操縱數據庫的Web服務。最終用戶將使用的軟件然后使用用戶的憑據簡單地使用Web服務對自身進行身份驗證,然后使用它來訪問資源。
現在我有以下問題,我的服務器在linux ubuntu上運行,並且已經存儲了一個使用plesk創建的網站。
我知道我可以使用http://www.mono-project.com/在linux上托管web服務。 但我從來沒有這樣做過,因為我總是使用PHP來做這些事情,而且我對如何將ac #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" }
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織網-服務-使用的PHP中之分
有很多方法可以從C#中使用Web服務。 RestSharp可能是理想的。
請注意,您可能會選擇SOAP或JSON作為格式。 因此,請查找使用相同格式的Web服務教程和使用者教程。 (構建一個可以返回不同格式的Web服務,具體取決於客戶端請求的理想情況,但更高級。我在PHP中沒有相關的教程)
(在此上下文中,“API”與“Web服務”同義,但請注意“API”也可以具有完全不同的含義)
你可能看起來還有很長的路要走,但不要擔心,一旦你的第一個例子工作,用同樣的方法做各種好東西都會很快捷方便。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.