简体   繁体   中英

WebBrowser.Navigate sending post data to PHP

I have a simple cli program that gets a mac address and machine name, creates a post string containing this information, and attempts to send it via POST using .net's WebBrowser.Naviate method. Everything seems to be working except for the POST data being sent. Whenever I print out the contents of PHP's Post array, I get an empty array. Have I missed a step somewhere?

C# Console Application

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
using System.Net.NetworkInformation;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Web;
using System.Windows.Forms;
using System.Threading;


namespace machine_boot
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                // Get the system mac address
                string theMac = GetMacAddress();

                System.Threading.ThreadStart objThreadStart = delegate
                {

                    string postData = "mm=" + theMac + "&mn=" + System.Environment.MachineName;
                    WebBrowser browserObject = new WebBrowser();
                    browserObject.Navigate(
                        "https://siteurl.com/test.php",
                        "_blank",
                        System.Text.Encoding.UTF8.GetBytes(postData),
                        "Content-Type: application/x-www-form-urlencoded" + Environment.NewLine
                    );

                };
                Thread theThread = new Thread(objThreadStart);
                theThread.SetApartmentState(ApartmentState.STA);
                theThread.Start();

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine("Contact an administrator");
                Console.WriteLine();
                Console.WriteLine("Press any key exit...");
                Console.ReadLine();
            }



        }

        /// <summary>
        /// Finds the MAC address of the NIC with maximum speed.
        /// </summary>
        /// <returns>The MAC address.</returns>
        static string GetMacAddress()
        {
            const int MIN_MAC_ADDR_LENGTH = 12;
            string macAddress = string.Empty;
            long maxSpeed = -1;

            foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
            {

                string tempMac = nic.GetPhysicalAddress().ToString();
                if (nic.Speed > maxSpeed &&
                    !string.IsNullOrEmpty(tempMac) &&
                    tempMac.Length >= MIN_MAC_ADDR_LENGTH)
                {
                    maxSpeed = nic.Speed;
                    macAddress = tempMac;
                }
            }

            return macAddress;
        }

    }

    /*
     * The class we map the json server response to
     */
    [DataContract]
    public class ServerResponse
    {
        [DataMember]
        public string machineurl { get; set; }
    }

}

siteurl.com/test.php

echo "<pre>";
echo var_export($_POST, true);

在此处输入图片说明

UPDATE

I located the access.log file for apache and did some experimenting. I found the initial POST request:

"POST /test.php HTTP/1.1" 200 0 325 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"

I then removed the second get parameter from postData variable in order to see if the request size changed which as you can see below it did:

"POST /test.php HTTP/1.1" 200 0 296 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"

So I know a POST request is being made, and that the POST data is contained inside the request. Could there be an issue with apache parsing the byte array? It might also be worth noting that every time a POST request is made an additional GET request is made immediately after which I thought was strange:

"POST /test.php HTTP/1.1" 200 0 325 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"
"GET /test.php HTTP/1.1" 200 0 269 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"

I think you should lose a ? in the beginning of postData .

Question mark is being used for separating path from query parameters in GET request.

For POST request, specifically application/x-www-form-urlencoded that you are using, you just need to pass URL-encoded parameters, forming key=value pairs, separated by & . No need for ? .

Also, try changing last argument to "Content-Type: application/x-www-form-urlencoded" + Environment.NewLine .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM