[C#] api post request problem

Recommended Posts

I have a code:

private static string PostRequest(string text)
            WebRequest request = WebRequest.Create("");
            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();
            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: 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:

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("", content).Result;
                //var response = client.GetAsync("").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:


$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 ('', 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'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.