Note that there are some explanatory texts on larger screens.

plurals
  1. POHttpRequest Exception Header section VB.NET
    text
    copied!<p>I'm trying to upload a file with the following code, but when I upload a file raises an exception: Header section has more than 10240 bytes (maybe it is not properly terminated)</p> <p>I try with this but isn't work: <a href="https://stackoverflow.com/questions/1874410/webrequest-post-with-both-file-and-parameters">WebRequest POST with both file and parameters</a></p> <pre><code>Dim boundary As String = "---------------------------" &amp; DateTime.Now.Ticks.ToString("x") Dim newLine As String = System.Environment.NewLine Dim boundaryBytes As Byte() = Encoding.ASCII.GetBytes(newLine &amp; "--" &amp; boundary &amp; newLine) Dim request As Net.HttpWebRequest = Net.WebRequest.Create(uri) request.ContentType = "multipart/form-data; boundary=" &amp; boundary request.Method = "POST" request.KeepAlive = True Using requestStream As IO.Stream = request.GetRequestStream() Dim formDataTemplate As String = "Content-Disposition: form-data; name=""{0}""{1}{1}{2}" For Each key As String In otherParameters.Keys requestStream.Write(boundaryBytes, 0, boundaryBytes.Length) Dim formItem As String Dim formItemBytes As Byte() If key = "file" Then formItemBytes = StreamFile(otherParameters(key)) Else formItem = String.Format(formDataTemplate, key, newLine, otherParameters(key)) formItemBytes = Encoding.UTF8.GetBytes(formItem) End If requestStream.Write(formItemBytes, 0, formItemBytes.Length) Next key requestStream.Write(boundaryBytes, 0, boundaryBytes.Length) Dim headerTemplate As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""{2}Content-Type: {3}{2}{2}" Dim header As String = String.Format(headerTemplate, fileParameterName, filePath, newLine, contentType) Dim headerBytes As Byte() = Encoding.UTF8.GetBytes(header) requestStream.Write(headerBytes, 0, headerBytes.Length) Using fileStream As New IO.FileStream(filePath, IO.FileMode.Open, IO.FileAccess.Read) Dim buffer(4096) As Byte Dim bytesRead As Int32 = fileStream.Read(buffer, 0, buffer.Length) Do While (bytesRead &gt; 0) requestStream.Write(buffer, 0, bytesRead) bytesRead = fileStream.Read(buffer, 0, buffer.Length) Loop End Using Dim trailer As Byte() = Encoding.ASCII.GetBytes(newLine &amp; "--" + boundary + "--" &amp; newLine) requestStream.Write(trailer, 0, trailer.Length) End Using Dim response As Net.WebResponse = request.GetResponse() </code></pre> <p><strong>EDIT:</strong></p> <p>I try with two options and not work:</p> <pre><code>Dim headerTemplate As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""{2} Content-Type: {3}{2}{2}" Dim headerTemplate As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""{2}" &amp; newLine &amp; "Content-Type: {3}{2}{2}" </code></pre>
 

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