Author Topic: Upload programatically on HFS? (C#)  (Read 3439 times)

0 Members and 1 Guest are viewing this topic.

Offline DMP9_MC

  • Occasional poster
  • *
  • Posts: 6
    • View Profile
Upload programatically on HFS? (C#)
« on: February 15, 2015, 06:08:36 PM »
Hi...

I dont know if anyone here is a programmer, and more specifically C#, but im trying to upload a file to HFS using C# WebClient. Heres my code:

            Console.Write("\nPlease enter the URI to post data to : ");
            String uriString = Console.ReadLine();

            // Create a new WebClient instance.
            WebClient myWebClient = new WebClient();

            Console.WriteLine("\nPlease enter the fully qualified path of the file to be uploaded to the URI");
            string fileName = Console.ReadLine();
            Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString);

            // Upload the file to the URI.
            // The 'UploadFile(uriString,fileName)' method implicitly uses HTTP POST method.
            byte[] responseArray = myWebClient.UploadFile(uriString, "POST", fileName);

            // Decode and display the response.
            Console.WriteLine("\nResponse Received.The contents of the file uploaded are:\n{0}",
                System.Text.Encoding.ASCII.GetString(responseArray));

When i do this, the remote server returns 405 Method not allowed.

Can anyone help? Please?

Offline bmartino1

  • Tireless poster
  • ****
  • Posts: 788
  • I'm only trying to help i mean no offense.
    • View Profile
    • none - google translate
Re: Upload programatically on HFS? (C#)
« Reply #1 on: February 18, 2015, 02:13:52 AM »
my first guess is that you need authentication to access the area to upload/write

otherwise, i'm pretty sure its:

i would have to guess that it is an issue with the c# code connecting to a program using a pascal file... program language incompatibility.. (not for sure!)

Hfs is made with Delphi....

try some info with:
http://wiki.freepascal.org/File_Handling_In_Pascal
« Last Edit: February 18, 2015, 02:15:28 AM by bmartino1 »
I'm only trying to help i mean no offense.
thank you for your time and patience,
Bmartino1

Offline rejetto

  • Administrator
  • Tireless poster
  • *
  • Posts: 12888
    • View Profile
Re: Upload programatically on HFS? (C#)
« Reply #2 on: February 21, 2015, 02:23:59 PM »
no, the programming language is not a problem.
There's no single way to upload a file through HTTP, and to be compatible with HFS you need to do it the right way.
It's not clear how that UploadFile method works, actually.
Try to post a dump of what it sends.
Or dump what a real browser does, and look at the differences, and try to find the method that fits better.

Follow members gave a thank to your post:


Offline DMP9_MC

  • Occasional poster
  • *
  • Posts: 6
    • View Profile
Re: Upload programatically on HFS? (C#)
« Reply #3 on: March 01, 2015, 04:19:18 PM »
Ok. I'm no longer using the UploadFile method. I'm using the same method i used to make a JSON POST request, but customizing it

Offline DMP9_MC

  • Occasional poster
  • *
  • Posts: 6
    • View Profile
Re: Upload programatically on HFS? (C#)
« Reply #4 on: March 01, 2015, 04:37:32 PM »
After doing the code below, it didn't register as an upload. Instead it registered as a GET, not POST.


            try
            {
                var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://PlusAPI:dmp9hosting_@dmp9software.ngrok.com/DMP9%20Hosting%20builds");
                httpWebRequest.ContentType = "application/json";
                httpWebRequest.Method = "POST";

                using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
                {
                    string json = "{\"agent\":{\"name\":\"Minecraft\",\"version\":1},\"username\":\"" + username + "\",\"password\":\"" + password + "\",\"clientToken\":\"6c9d237d-8fbf-44ef-b46b-0b8a854bf391\"}";

                    streamWriter.Write(json);
                    streamWriter.Flush();
                    streamWriter.Close();

                    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                    {
                        var result = streamReader.ReadToEnd();
                        Console.WriteLine(result);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.Read();

Offline rejetto

  • Administrator
  • Tireless poster
  • *
  • Posts: 12888
    • View Profile
Re: Upload programatically on HFS? (C#)
« Reply #5 on: March 14, 2015, 11:51:11 AM »
JSON is not supported by HFS.
If you dump the request you are trying to send (the other one, not the json) i can tell you what's wrong with it.
The source code is not telling enough.

Offline DMP9_MC

  • Occasional poster
  • *
  • Posts: 6
    • View Profile
Re: Upload programatically on HFS? (C#)
« Reply #6 on: April 18, 2015, 09:20:44 AM »
    public void PostMultipleFiles(string url, string[] files)
{
    string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");
    HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
    httpWebRequest.ContentType = "multipart/form-data; boundary=" + boundary;
    httpWebRequest.Method = "POST";
    httpWebRequest.KeepAlive = true;
    httpWebRequest.Credentials = System.Net.CredentialCache.DefaultCredentials;
    Stream memStream = new System.IO.MemoryStream();
    byte[] boundarybytes =System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary     +"\r\n");
    string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition:  form-data; name=\"{0}\";\r\n\r\n{1}";
    string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n Content-Type: application/octet-stream\r\n\r\n";
    memStream.Write(boundarybytes, 0, boundarybytes.Length);
    for (int i = 0; i < files.Length; i++)
    {
        string header = string.Format(headerTemplate, "file" + i, files);
        //string header = string.Format(headerTemplate, "uplTheFile", files);
        byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
        memStream.Write(headerbytes, 0, headerbytes.Length);
        FileStream fileStream = new FileStream(files, FileMode.Open,
        FileAccess.Read);
        byte[] buffer = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            memStream.Write(buffer, 0, bytesRead);
        }
        memStream.Write(boundarybytes, 0, boundarybytes.Length);
        fileStream.Close();
    }
    httpWebRequest.ContentLength = memStream.Length;
    Stream requestStream = httpWebRequest.GetRequestStream();
    memStream.Position = 0;
    byte[] tempBuffer = new byte[memStream.Length];
    memStream.Read(tempBuffer, 0, tempBuffer.Length);
    memStream.Close();
    requestStream.Write(tempBuffer, 0, tempBuffer.Length);
    requestStream.Close();
    try
    {
        WebResponse webResponse = httpWebRequest.GetResponse();
        Stream stream = webResponse.GetResponseStream();
        StreamReader reader = new StreamReader(stream);
        string var = reader.ReadToEnd();

    }
    catch (Exception ex)
    {
        response.InnerHtml = ex.Message;
    }
    httpWebRequest = null;
}

Follow members gave a thank to your post:


Offline DMP9_MC

  • Occasional poster
  • *
  • Posts: 6
    • View Profile
Re: Upload programatically on HFS? (C#)
« Reply #7 on: April 18, 2015, 09:22:11 AM »
^ Thats my original code

Offline rejetto

  • Administrator
  • Tireless poster
  • *
  • Posts: 12888
    • View Profile
Re: Upload programatically on HFS? (C#)
« Reply #8 on: May 01, 2015, 02:56:49 PM »
it's hard for me to help you with your source code.
your options are
1. since the browser works, you sniff what the browser is sending, and change your way to be closer to it
2. you post a small dump of 2 small text files, and i can check it and tell you what's bad in it