Tigran
Tigran

Reputation: 1057

Uploading file to web service via post

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?

Upvotes: 0

Views: 1075

Answers (2)

bashkan
bashkan

Reputation: 464

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

Upvotes: 2

Mattias &#197;slund
Mattias &#197;slund

Reputation: 3907

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

Upvotes: 0

Related Questions