我们可以在https上将数据从VB.Net发布到CakePHP吗?使用下面的代码,它只显示“GET”而不是“POST”响应,所以如果使用$ this-> request-> getData(),将返回空数组。
注意:
VB.Net应用代码:
'ByPass SSL Certificate Validation Checking
System.Net.ServicePointManager.ServerCertificateValidationCallback = _
Function(se As Object, _
cert As System.Security.Cryptography.X509Certificates.X509Certificate, _
chain As System.Security.Cryptography.X509Certificates.X509Chain, _
sslerror As System.Net.Security.SslPolicyErrors) True
'Call web application/web service with HTTPS URL
Dim request As WebRequest = WebRequest.Create("https://localhost/testapp/test/")
' Set the Method property of the request to POST.
request.Method = "POST"
' Create POST data and convert it to a byte array.
Dim postData As String = "name=yos"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
' Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded"
' Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length
' Get the request stream.
Dim dataStream As Stream = request.GetRequestStream()
' Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length)
' Close the Stream object.
dataStream.Close()
' Get the response.
Dim response As WebResponse = request.GetResponse()
' Display the status.
Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
' Get the stream containing content returned by the server.
dataStream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
' Display the content.
Console.WriteLine(responseFromServer)
' Clean up the streams.
reader.Close()
dataStream.Close()
response.Close()
txtResponse.Text = responseFromServer
'Restore SSL Certificate Validation Checking
System.Net.ServicePointManager.ServerCertificateValidationCallback = Nothing
CakePHP代码:
TestController.php
class TestController extends AppController {
public function index(){
$this->autoRender = false;
print_r($this->request->getMethod());
print_r($this->request->getData());
}
}
AppController.php
class AppController extends Controller
{
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Security', ['blackHoleCallback' => 'forceSSL']);
}
public function beforeRender(Event $event)
{
if (!array_key_exists('_serialize', $this->viewVars) &&
in_array($this->response->type(), ['application/json', 'application/xml'])
) {
$this->set('_serialize', true);
}
}
public function beforeFilter(Event $event)
{
$this->Security->requireSecure();
}
public function forceSSL()
{
return $this->redirect('https://' . env('SERVER_NAME') . $this->request->here());
}
}
服务器响应
GET //should be POST
Array() //should be Array([name] => yos)