I try to upload a file from C# to a server but it does not work properly. I wrote the following code:
string adress2send = "http://ip_adress"
byte[] buff = null;
FileStream fs = new FileStream(file2upload,FileMode.Open,FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(file2upload).Length;
buff = br.ReadBytes((int)numBytes);
byte[] file = buff;
string contentType = "application/octet-stream";
string boundary = "------------------------" + DateTime.Now.Ticks.ToString("x");
using (WebClient wc = new WebClient())
{
wc.Headers.Add("Content-Type", "multipart/form-data; boundary=" + boundary);
var fileData = wc.Encoding.GetString(file);
var package = string.Format("--{0}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n{3}\r\n--{0}--\r\n", boundary, adress2send, contentType, fileData);
var nfile = wc.Encoding.GetBytes(package);
byte[] resp = wc.UploadData(adress2send, "POST", nfile);
}
In principle it works. The file is properly loaded to the server and the server uses it. But I get an error message at:
byte[] resp = wc.UploadData(adress2send, "POST", nfile)
:
System.Net.WebException: "Der Server hat eine Protokollverletzung ausgeführt.. Section=ResponseStatusLine"
I think this happens because I wait for a response and I get no response? Is there a way to send the data and not wait for a response? Or is this a server-bug or a code-bug? Is there an alternative to do that?
I tried to do that with WebRequest, but this does not work: The file is not loaded to the server. I read a lot of other threads like: Link1, Link2 And a lot of others, but I can not find a solution.