Note that there are some explanatory texts on larger screens.

plurals
  1. POError while reading response from HttpWebRequest
    primarykey
    data
    text
    <p>I am trying to send contents of 1GB text file over the network. I modified the <a href="http://support.microsoft.com/kb/908573" rel="nofollow"> suggested code </a> for basic authentication and kept it as follows :</p> <pre><code> WRequest = (HttpWebRequest)HttpWebRequest.Create(URL); WRequest.Credentials = Credentials; WRequest.PreAuthenticate = true; WRequest.ContentType = "text/plain"; WRequest.Method = "POST"; WRequest.AllowWriteStreamBuffering = false; WRequest.Timeout = 10000; FileStream ReadIn = new FileStream(filename, FileMode.Open, FileAccess.Read); ReadIn.Seek(0, SeekOrigin.Begin); WRequest.ContentLength = ReadIn.Length; Byte[] FileData = new Byte[ReadIn.Length]; int DataRead = 0; Stream tempStream = WRequest.GetRequestStream(); do { DataRead = ReadIn.Read(FileData, 0, 2048); if (DataRead &gt; 0) { tempStream.Write(FileData, 0, DataRead); Array.Clear(FileData, 0, 2048); } } while (DataRead &gt; 0); // The response WResponse = (HttpWebResponse)WRequest.GetResponse(); </code></pre> <p>However, now it gives me System.Net.ProtocolViolationException error : "You must write ContentLength bytes to the request stream before calling [Begin]GetResponse". I checked <a href="http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.begingetresponse.aspx" rel="nofollow">HttpWebRequest.BeginGetRequestResponse </a>... and found from debugging that the contentlength for WRequest is not -1. What else could be going wrong ? How should I get the response ?</p> <p><strong>Update</strong> : The code which worked for small files is as followed :</p> <pre><code> WebRequest request = WebRequest.Create(url); request.Method = "POST"; request.Credentials = Credentials; using (StreamReader reader = new StreamReader(filename)) { postData = reader.ReadToEnd(); } byte[] byteArray = Encoding.UTF8.GetBytes(postData); request.ContentType = "text/plain"; request.ContentLength = byteArray.Length; Stream dataStream = request.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); // The response WebResponse response = request.GetResponse(); Console.WriteLine(((HttpWebResponse)response).StatusDescription); dataStream = response.GetResponseStream(); using (StreamReader reader = new StreamReader(dataStream)) { responseFromServer = reader.ReadToEnd(); } dataStream.Close(); response.Close(); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload