Search code examples
http-postwebrequestmaxscript

Send a String to server with http POST


I have a maxScript that requires to send the mac address of the user to a server and check if it is included in the allowed list.
I have to part fo getting the mac address and the server side is all set.
the only problem is that I want it to be sent by POST so there would be more security to it but I have no idea how to do that.


Solution

  • Okay, I finally figured it out. here is the complete code for getting the mac address and sending it to a server with HttpPost:

    
        --Getting the mac address
            NI = dotnetclass "System.Net.NetworkInformation.NetworkInterface";
            NI.GetIsNetworkAvailable();
            ALL = NI.GetAllNetworkInterfaces();
            MACAddress = ALL[1].GetPhysicalAddress();
            print (MACAddress.toString());
        --Encoding the mac address so it is sendable
            A = (dotNetClass "System.Text.Encoding");
            PostData = "macaddress=" + MACAddress.toString();
            MData = A.ASCII.GetBytes (PostData);
        --Creating the Post request
            Req = (dotNetClass "System.Net.WebRequest").Create ("http://ip.mdfplan.com/");
            Req.Method = "Post";
            Req.ContentType = "application/x-www-form-urlencoded";
            Req.ContentLength = MData.count;
        --Writing the data in the request
            S = Req.GetRequestStream();
            S.write MData 0 MData.count;
            S.close();
        --Sending the request and recieving the response
            Res = Req.GetResponse();
            ResStr = Res.GetResponseStream();
        --Reading the respone
            objReader = dotnetobject "System.IO.StreamReader" ResStr;
            ResText = (objReader.ReadToEnd());