0

I need to write a httpclient to upload a huge file to the server using http. The code

public Stream function()
{
    string postData = "This is a test that posts this string to a Web server.";
    byte[] byteArray = Encoding.UTF8.GetBytes (postData);

    // Set the ContentType property of the WebRequest.
    request.ContentType = "application/x-www-form-urlencoded";

    // Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length;
    request.Method = "POST";

    // Get the request stream.
    Stream dataStream = request.GetRequestStream ();

    // Write the data to the request stream.
    return dataStream;
}

Then, I return the dataStream to the caller of this function so that the application keeps writing to the stream using write. Then once done with the writing, GetResponse is called and then the stream is closed. But I get the exception

ProtocolViolationException at System.Net.HttpWriteStream.Write() while writing to the stream.

Kindly help.

user1029660
  • 53
  • 1
  • 5
  • Do you have any control about the server. Simply whipping up a server to check what error messages are thrown can be a huge help while debugging. – CodingBarfield Mar 30 '12 at 06:33
  • possible duplicate of [Upload files with HTTPWebrequest (multipart/form-data)](http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data) – jgauffin Mar 30 '12 at 06:35
  • 1
    @CodingBarfield: Files can't be uploaded with `application/x-www-form-urlencoded` – jgauffin Mar 30 '12 at 06:35
  • The server is able to create the file, but since the data isnt available it gives me the exception "The I/O operation has been aborted because of either a thread exit or an application request" – user1029660 Mar 30 '12 at 06:39
  • @jgauffin - The link you had mentioned is not the one i am looking for – user1029660 Mar 30 '12 at 10:29
  • You might want to explain why. – jgauffin Mar 30 '12 at 10:48

2 Answers2

0

The ProtocolViolationException is probably thrown because the request method is GET or HEAD.

You must set the Method to POST like this:

// Set the 'Method' property of the 'Webrequest' to 'POST'.
request.Method = "POST";

See HttpWebRequest.GetRequestStream Method documentation on MSDN.

Remko Jansen
  • 4,649
  • 4
  • 30
  • 39
  • The MSDN documentation mentions other conditions that can cause a ProtocolViolationException during a POST like: KeepAlive is true, AllowWriteStreamBuffering is false, SendChunked is false. Did you check that? Also, did you close the request stream before calling GetResponse()? – Remko Jansen Mar 30 '12 at 06:52
  • Yes. Those conditions have been checked. – user1029660 Mar 30 '12 at 06:56
  • you say you return the request stream to the caller of this function so that the application keeps writing to the stream. Do you write the exact number of bytes to the request stream as set in request.ContentLength? Also, did you close the request stream before calling GetResponse()? – Remko Jansen Mar 30 '12 at 07:05
  • when sendChunked is set to false, the number of bytes need not be set. Also since i will be keep writing to the stream I will not know the exact number of bytes. – user1029660 Mar 30 '12 at 08:59
  • Ok, now i'm also learning something new. I never really looked into what SendChunked does. I'm running out of ideas. Did you use tools like Fiddler to see what HTTP request/responses are actually send? – Remko Jansen Mar 30 '12 at 09:21
0

Have you considered using Fiddler to debug the process when using a browser?
This is a simple proxy allowing you to inspect http traffic.

weismat
  • 7,195
  • 3
  • 43
  • 58