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>
&开发者_JS百科lt;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 :)
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)
精彩评论