0

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.

pushkin
  • 9,575
  • 15
  • 51
  • 95
Loui
  • 11
  • 2
  • Have you checked your `var package = string.Format(...)` values? Anyway, I would go with this: [UploadFile with POST values by WebClient](https://stackoverflow.com/questions/11048258/uploadfile-with-post-values-by-webclient). Not really about WebClient, though. Also, note that `WebClient.Encoding` uses the local encoding (your system codepage). Maybe it's not what's expected on the other side. – Jimi Jul 20 '18 at 00:59
  • Do you think that this is a package-problem? Because the code above is able to send the file to the server and the server obviously gets and uses he data. The problem is, that the file is properly send, but I get the error message and that I am not able to send this package with – Loui Jul 20 '18 at 09:11
  • The problem with the code above is, that the file is properly send, but I get the error message. Or can this still be a package problem? On the other hand I tried to rewrite the code above with WebRequest and there I failed totally, because the server is not able to use the same package. But I will have a look at your link and test this. Additionally it is important to know that I am using local encoding. Thanks a lot ! – Loui Jul 20 '18 at 09:20

0 Answers0