Upload file Error System.Net.ProtocolViolationException: The contentlength byte must be written to the request stream before calling [Begin]getresponse.

Source: Internet
Author: User

Error when uploading a file.

Error:

System.Net.ProtocolViolationException: The contentlength byte must be written to the request stream before calling [Begin]getresponse.

Cause: The length of the pass is inconsistent with the contentlength you write in the header file.

Workaround:

Ensure that the contentlength is consistent with the transmitted data length.

Instance:

FileStream filestream= New FileStream (FilePath, FileMode.Open, FileAccess.Read, Fileshare.readwrite))
byte[] Sendstreamdata = Streamtobyte (signfile);//Transfer files into byte[]

Post (FileStream);//This function is used to transfer the file stream, here will be an error, because the current stream's initial position is the end position.

The reason I showed this file is that the file stream has been read by me and has not been repositioned.

Plus the positioning code, problem solving.

Modify the following:

FileStream filestream= New FileStream (FilePath, FileMode.Open, FileAccess.Read, Fileshare.readwrite))
byte[] Sendstreamdata = Streamtobyte (signfile);//Transfer files into byte[]

Filestream.seek (0, Seekorigin.begin);//position to initial position. The new Code

Post (FileStream);//This function is used to transfer file streams,

Upload file Error System.Net.ProtocolViolationException: The contentlength byte must be written to the request stream before calling [Begin]getresponse.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.