JeViCo

[C#] luac.mtasa.com api post request problem

Recommended Posts

I have a code:

private static string PostRequest(string text)
        {
            WebRequest request = WebRequest.Create("http://luac.mtasa.com/index.php");
            request.Method = "POST";
            string data = "compile=1&debug=0&obfuscate=3&luasource=" + text;
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;

            using (Stream dataStream = request.GetRequestStream())
            {
                dataStream.Write(byteArray, 0, byteArray.Length);
            }

            WebResponse response = request.GetResponse();
            string resp;
            using (Stream stream = response.GetResponseStream())
            {
                using (StreamReader reader = new StreamReader(stream))
                {
                    resp = reader.ReadToEnd();
                }
            }
            response.Close();
            return resp;
        }

which should return compiled and obfuscates version of text but there is a problem - it always returns "ERROR Could not compile file" even if i use "local a = 10" string as a text.

I guess that problem hides in ContentType variable and urlencoded maybe ruins Lua code however i don't know how to fix it right now.

Share this post


Link to post

According to the MSDN, WebResponse should not be used for new projects (see: https://docs.microsoft.com/en-us/dotnet/api/system.net.webrequest?view=netframework-4.8). Instead, we have System.Net.Http.HttpClient now.

That function allows you to conviniently pass in form-encoded data via a Dictionary. See here for an example: https://stackoverflow.com/a/4015346/10035431

Share this post


Link to post

Hello, thanks for reply! I've tried to implement that method and got the same result. Updated code:

using System.Net.Http;

private string PostRequest(string text)
        {
            var values = new Dictionary<string, string> {
                { "compile", "1" },
                { "debug", "0" },
                { "obfuscate", "3" },
                { "luasource", "local a = 10" } // example code
            };

            using (var client = new HttpClient())
            {
                var content = new FormUrlEncodedContent(values);
                var response = client.PostAsync("http://luac.mtasa.com/index.php", content).Result;
                //var response = client.GetAsync("http://google.com").Result;

                if (response.IsSuccessStatusCode)
                {
                    var responseContent = response.Content;

                    // by calling .Result you are synchronously reading the result
                    string resp = responseContent.ReadAsStringAsync().Result;

                    Console.WriteLine(resp); // ERROR Could not compile file
                    return resp;
                } else {
                    return "Error connecting website";
                }
            }
        }

I had some problems with async methods in sync thread however i fixed it somehow

Share this post


Link to post

It seems that api does not support programming-based remote access. PHP script has same result - "ERROR Could not compile file". Code:

<?php

$data = array(
	'compile' => '1',
	'debug' => '0',
	'obfuscate' => '3',
	'luasource' => 'print("Hello World!")'
);

$query = http_build_query ($data);
$contextData = array (
            'method' => 'POST',
            'header' => "Content-type: application/x-www-form-urlencoded\r\n".
            			"Connection: close\r\n".
                        "Content-Length: ".strlen($query)."\r\n",
            'content'=> $query );

$context = stream_context_create (array ( 'http' => $contextData ));

$result =  file_get_contents ('http://luac.mtasa.com/index.php', false, $context);
echo $result;

 

Share this post


Link to post

Solved, my bad. So what was wrong?

1. I was trying to send code as plain text. I had to send a file, not a text.
2. I thought content type had to be "application/x-www-form-urlencoded". Nope - "multipart/form-data" (found in luac.mtasa.com's web page source code)
3. UTF-BOM and UTF without BOM didn't work for me so i used Encoding.Default everywhere (fixed StreamWriter too)

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.