0
我想使用C#客戶端來驗證用戶,但沒有任何工作。我是新的Visual Studio,所以我不知道所有的功能; /我的基本按鍵事件:C#客戶端身份驗證PHP肥皂服務器
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
localhost.Calculator client = new localhost.Calculator();
string returnString = client.Average(10,8);
//System.Diagnostics.Debug.Write(rep);
label1.Text = returnString;
}
}
}
我server.php文件:
<?php
ini_set("soap.wsdl_cache_enabled", "0");
class Server{
protected $class_name = '';
public function __construct($class_name)
{
$this->class_name = $class_name;
}
public function authenticate ($username, $password)
{
if ($username != 'admin' && $password != 'admin') return false;
else return true;
}
public function log($method_name,$data)
{
$filename = 'log.txt';
$handle = fopen($filename, 'a+');
fwrite($handle, date("l dS of F Y h:i:s A").' - '.$_SERVER['REMOTE_ADDR']."\r\n".$method_name."\r\n".print_r($data,true));
fclose($handle);
}
public function __call($method_name, $parameters)
{
$this->log($method_name,$parameters); // log
if(!$this->authenticate($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']))
{
}
else
{
if(!method_exists($this->class_name, $method_name))
return 'Method '.$method_name.' not found'; // methot exist check
return call_user_func_array(
array($this->class_name, $method_name),
$parameters
); //call method
}
}
}
class Calculator {
public function Average ($parameters)
{
$num1 = $parameters->num1;
$num2 = $parameters->num2;
return self::AverageResponse(($num1 + $num2)/2);
}
public function AverageResponse ($message)
{
return ['Result' => $message];
}
public function Minus ($parameters)
{
$num1 = $parameters->num1;
$num2 = $parameters->num2;
return self::MinusResponse($num1 - $num2);
}
public function MinusResponse ($message)
{
return ['Result' => $message];
}
}
class in {
}
$Service = new Server('Calculator');
$classmap=[
'in' => 'in'
];
$server = new SOAPServer('http://localhost:9080/soap-websiteservice-wsdl/Calculator.wsdl', array(
'soap_version' => SOAP_1_2,
'style' => SOAP_RPC,
'use' => SOAP_LITERAL,
'classmap'=>$classmap
));
$server->setObject($Service);
//$server->setClass('Calculator');
$server->handle();
PHP客戶端的認證工作。代碼:
<?php
// client.php
$options = array(
'trace' => true,
'login' => 'admin',
'password' => 'admin'
);
$client = new SOAPClient('http://localhost:9080/soap-websiteservice-wsdl/server.php?wsdl', $options);
echo "Average: \r\n";
var_dump($client->Average(['num1' => 10, 'num2' => 6])->Result);
echo "Minus: \r\n";
var_dump($client->Minus(['num1' => 10, 'num2' => 6])->Result);
WSDL:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost:9080/soap-websiteservice-wsdl/Calculator" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Calculator" targetNamespace="http://localhost:9080/soap-websiteservice-wsdl/Calculator">
<wsdl:types>
<xsd:schema targetNamespace="http://localhost:9080/soap-websiteservice-wsdl/Calculator">
<xsd:element name="Average">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="num1" type="xsd:int" />
<xsd:element name="num2" type="xsd:int"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="AverageResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Result" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="AverageRequestType">
<xsd:sequence>
<xsd:element name="num1" type="xsd:int"></xsd:element>
<xsd:element name="num2" type="xsd:int"></xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:element name="Minus">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="num1" type="xsd:int"></xsd:element>
<xsd:element name="num2" type="xsd:int"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="MinusResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="Result" type="xsd:string"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="AverageFault">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="AverageFault" type="xsd:string"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="AverageFault1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="AverageFault" type="xsd:string"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="AverageRequest">
<wsdl:part element="tns:Average" name="parameters"/>
</wsdl:message>
<wsdl:message name="AverageResponse">
<wsdl:part element="tns:AverageResponse" name="parameters"/>
</wsdl:message>
<wsdl:message name="MinusRequest">
<wsdl:part name="parameters" element="tns:Minus"></wsdl:part>
</wsdl:message>
<wsdl:message name="MinusResponse">
<wsdl:part name="parameters" element="tns:MinusResponse"></wsdl:part>
</wsdl:message>
<wsdl:message name="AverageFault">
<wsdl:part name="parameters" element="tns:AverageFault"></wsdl:part>
</wsdl:message>
<wsdl:message name="AverageFault1">
<wsdl:part name="parameters" element="tns:AverageFault1"></wsdl:part>
</wsdl:message>
<wsdl:portType name="Calculator">
<wsdl:operation name="Average">
<wsdl:input message="tns:AverageRequest"/>
<wsdl:output message="tns:AverageResponse"/>
</wsdl:operation>
<wsdl:operation name="Minus">
<wsdl:input message="tns:MinusRequest"></wsdl:input>
<wsdl:output message="tns:MinusResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CalculatorSOAP" type="tns:Calculator">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="Average">
<soap:operation
soapAction="http://localhost:9080/soap-websiteservice-wsdl/Calculator/NewOperation" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="Minus">
<soap:operation
soapAction="http://localhost:9080/soap-websiteservice-wsdl/Calculator/Minus" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Calculator">
<wsdl:port binding="tns:CalculatorSOAP" name="CalculatorSOAP">
<soap:address location="http://localhost:9080/soap-websiteservice-wsdl/server.php"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
我假設你收到一條錯誤消息,你可能想要包含它。 –
這裏沒有錯誤。我只是不知道從c#客戶端發送登錄和密碼到php肥皂服務器。在例子中,你可以看到PHP客戶端,我可以輕鬆地發送登錄名和密碼 – Ernestyno