0

Hello I got a Problem sending a HTTP Request in C# I´d like to upload a File in a HTTP Request but I´m not sure how to do it

Here is the html Code:

<form action="/decrypt/upload" method="post" enctype="multipart/form-data">
            <fieldset>
                <p class="formrow file_upload">
                <label for="dlcfile">Container File</label>
                <input type="file" class="file_field" name="dlcfile" id="dlcfile"/>
                <input type="text" value="Click here to select a file..." class="file_overlay" />
                </p>
                <p class="buttonrow"><button type="submit">Submit »</button></p>
            </fieldset>
        </form>

And here is my C# Code:

public static void decryptContainer(string path)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://dcrypt.it/decrypt/upload");
        request.Method = "POST";
        request.ContentType = "application/x-www-form-urlencoded";


        using (StreamWriter writer = new StreamWriter(request.GetRequestStream(), Encoding.ASCII))
        {
            writer.Write("dlcfile=" + path);
        }

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            Console.WriteLine(reader.ReadToEnd());
        }
    }

I know that I have to give the parameter an File, but I just don´t know how to handel that in C#, could someone plx help me :)

Googles
  • 257
  • 2
  • 7
  • 11

1 Answers1

2

Instead of messing around with requests yourself, just use a WebClient. This is essentially a wrapper class for all of those HttpRequest classes and makes things so much easier. Your code will definitely get a lot simpler if you switch to this.

You should also take a look at this answer, which describes how to POST a file to a webserver with C#:

Send a file via HTTP POST with C#

Or, using Form data:

Upload files with HTTPWebrequest (multipart/form-data)

Community
  • 1
  • 1
qJake
  • 16,821
  • 17
  • 83
  • 135
  • Sorry but the other Post doesnt help, me as it´s said in the answer I got – Googles Oct 13 '11 at 17:51
  • Then try this: http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data – qJake Oct 13 '11 at 17:52