不能通过https上的VB.Net WebRequest将数据发布到CakePHP 3应用程序

时间:2017-10-01 10:15:09

标签: vb.net cakephp https cakephp-3.0 webrequest

我们可以在https上将数据从VB.Net发布到CakePHP吗?使用下面的代码,它只显示“GET”而不是“POST”响应,所以如果使用$ this-> request-> getData(),将返回空数组。

注意:

  • 发布在http上的CakePHP 3或https
  • 上的本机php上
  • 使用VB.Net 2008,目标框架:3.5
  • 使用CakePHP 3.5.3
  • Wamp Server 3.1.0 64bit
  • Apache 2.4.27 - PHP 5.6.31

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)

0 个答案:

没有答案