0

I am new to c# and I get problem with uploading file to web server.

I used this as template when generating my code.

try
{
    using (WebClient client = new WebClient()) {
        client.Headers.Add("Content-Type", "binary/octet-stream");
        ans = client.UploadFile(remoteSiteFileURL, "POST", @"D:\test.txt");
    }

    string responseAsString = Encoding.Default.GetString(ans);
    MessageBox.Show("Done" + responseAsString);
}

When I call this code, I get nothing, only exception after sometime (may be a minute) 'The operation has time out'.

File test.txt is less than 1kb (actually few bytes). Web service (site) works and I easily uploaded 500kb of data.

I used next html form when testing upload:

<h1>Upload file form</h1>
    <form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="2097152" />
        Send file<input name="userfile" type="file" />
    <input type="submit" value="Send File" />
    </form>

No auth, session control is set up in upload.php.

Update1: 1) When I run this code, I cannot close my application. It hang it. 2) I turned off firewall, this did not help.

Update2: I put a logger at 'upload.php' that writes to file when it was called. So, I have nothing in my log, i.e. it is not called (am not sure about it. may be it (script) will be called when full file transfered).

What the problem might be? Am I missing something?

Community
  • 1
  • 1
Tigran
  • 1,049
  • 3
  • 15
  • 31

2 Answers2

2

Solution is to increase this timout value. But the WebClient class doesn't have such property or method. So you need to wrap WebClient up by your own class to set the timeout of the base class before uploading data. Moreover u can try async method of WebClient. WebClient.UploadFileAsync

bashkan
  • 464
  • 1
  • 5
  • 14
  • But why? I have less than 1kb file, so it should transfer fast. – Tigran Apr 20 '14 at 16:25
  • pls check, be able to access the site that try to upload? – bashkan Apr 20 '14 at 16:40
  • Sure (I did calls via System.Net.WebRequest.Create(...)) to remote site. I recetly, did it locally at my server. And file transfer worked (!?!?), so have 3 possible explanations: 1) remote server is wrongly configured.(but my brouser form tranfer work) 2) My code do not send something (may be header or something) or 3) I have issue with this client (no idea which and why). – Tigran Apr 20 '14 at 16:45
  • Number one is make sense. No issue within code's flow, In any case you shuld use async operation such a network coding. – bashkan Apr 20 '14 at 17:07
  • The problem was in client. I saw no outgoing requests in wireshark [network trafic analyser]. In my code separately to webclient, I do some webrequests before, so I found out that if stop doing them, uploading works! I have no idea of real problem (lack of ports or number of components limitation or something else), but I add close() parameter to some places in my code and it run! – Tigran Apr 21 '14 at 00:37
0

This sample does not set the header first. Any more luck then?

Mattias Åslund
  • 3,877
  • 2
  • 18
  • 17
  • It also fail without setting headers. I found the good example and they do headers setup. - http://www.johny.org/2007/08/14/upload-using-c-as-client-and-php-as-server/ – Tigran Apr 20 '14 at 16:17