.NET Programming
--
Questions
--
Followers
Top Experts
Using vb.net, how to send POST request with post data and save the binary response to a file?
I am trying to write a console script that makes a POST request to a web server with post data and saves the response from the server to a file. The server response is a compressed tar archive. I'm able to make the request using weblclient upload data, but I cannot figure out a way to save the binary response to a variable and eventually to a file.
I am using vb.net framework 1.1.
Here is a snippet of my code so far:
' Download a single binary file from a server and save it to a local folder
Public Sub DownloadAndSaveFile(ByVal Url As String, ByVal Filename As String, ByVal User As String, ByVal Pass As String, ByVal Debug As Integer)
' The post data template
Dim PostdataTemplate As String = _
"handler=SOME_URLENCODED_D ATA"
Dim PostdataArray As Byte() = Encoding.ASCII.GetBytes(Po stdata)
' Create a new NetworkCredential object
Dim NetworkCredential As New NetworkCredential(User, Pass)
Try
' Create a new WebClient instance
Dim myWebClient As New WebClient
' Set Preauthenticate property to true
'myWebClient.PreAuthentica te = True
' Associate the NetworkCrbedential object with the 'WebRequest' object
myWebClient.Credentials = NetworkCredential
' Add required HTTP headers to request
myWebClient.Headers.Add("A ccept", "*/*")
myWebClient.Headers.Add("C ontent-Typ e", "application/x-www-form-ur lencoded")
' UploadData method implicitly sets HTTP POST as the request method
Dim responseArray As Byte() = myWebClient.UploadData(Url , PostdataArray)
' The response array as byte generates an exception!
Catch Ex As Exception
WriteLine("Error: " & Ex.Message)
End Try
End Sub
I get an exception when saving the response as a byte:
Error: The underlying connection was closed: The server committed an HTTP protocol violation.
If I change the server to respond with ascii text, the response is correctly saved. I think the issue is that I'm trying to save binary data to a variable that only handles text.
Can someone provide an example of how to send a POST request with parameters and then save the binary file that the server responds with to a local file?
Thanks!
I am using vb.net framework 1.1.
Here is a snippet of my code so far:
' Download a single binary file from a server and save it to a local folder
Public Sub DownloadAndSaveFile(ByVal Url As String, ByVal Filename As String, ByVal User As String, ByVal Pass As String, ByVal Debug As Integer)
' The post data template
Dim PostdataTemplate As String = _
"handler=SOME_URLENCODED_D
Dim PostdataArray As Byte() = Encoding.ASCII.GetBytes(Po
' Create a new NetworkCredential object
Dim NetworkCredential As New NetworkCredential(User, Pass)
Try
' Create a new WebClient instance
Dim myWebClient As New WebClient
' Set Preauthenticate property to true
'myWebClient.PreAuthentica
' Associate the NetworkCrbedential object with the 'WebRequest' object
myWebClient.Credentials = NetworkCredential
' Add required HTTP headers to request
myWebClient.Headers.Add("A
myWebClient.Headers.Add("C
' UploadData method implicitly sets HTTP POST as the request method
Dim responseArray As Byte() = myWebClient.UploadData(Url
' The response array as byte generates an exception!
Catch Ex As Exception
WriteLine("Error: " & Ex.Message)
End Try
End Sub
I get an exception when saving the response as a byte:
Error: The underlying connection was closed: The server committed an HTTP protocol violation.
If I change the server to respond with ascii text, the response is correctly saved. I think the issue is that I'm trying to save binary data to a variable that only handles text.
Can someone provide an example of how to send a POST request with parameters and then save the binary file that the server responds with to a local file?
Thanks!
Zero AI Policy
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
ASKER CERTIFIED SOLUTION
membership
Log in or create a free account to see answer.
Signing up is free and takes 30 seconds. No credit card required.
Hi udhayakumard,
You are correct. The server is responding with an HTTP header with no name or value. There is a colon, space and then carriage return and line feed.
I found that I could fix this by adding the following to either the machine.config or to MY_SCRIPT.exe.config:
-------------------------- ---------- ---------- ---------- ----------
<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="tr ue" />
</settings>
</system.net>
</configuration>
-------------------------- ---------- ---------- ---------- ----------
I'll award you the points regardless, but I'm wondering if there is a fix for this within the script I'm writing (that wouldn't require modifying an external config file or adding a new config file). The reason I ask is that I am providing this script executable to a third party and don't want them to have to make any further changes beyond running the script.
Thank you
You are correct. The server is responding with an HTTP header with no name or value. There is a colon, space and then carriage return and line feed.
I found that I could fix this by adding the following to either the machine.config or to MY_SCRIPT.exe.config:
--------------------------
<configuration>
<system.net>
<settings>
<httpWebRequest useUnsafeHeaderParsing="tr
</settings>
</system.net>
</configuration>
--------------------------
I'll award you the points regardless, but I'm wondering if there is a fix for this within the script I'm writing (that wouldn't require modifying an external config file or adding a new config file). The reason I ask is that I am providing this script executable to a third party and don't want them to have to make any further changes beyond running the script.
Thank you






EARN REWARDS FOR ASKING, ANSWERING, AND MORE.
Earn free swag for participating on the platform.
I should note that I know that the server inserting a header with no name or value is against RFC2616. But I don't have the ability to modify the server's response in the production environment.
Thanks...
Thanks...
U just place the script in ur customers server
Currently, the only method I know of to instruct vb to ignore the illegal header from the server is to add useUnsafeHeaderParsing="tr ue" to either the machine.config or MY_SCRIPT.exe.config.
I'd like to only provide the customer with the .exe file and not force them to include the .config file change.
Is there a way to include this configuration option in my executable so I only provide the customer with a single file?
Thanks
I'd like to only provide the customer with the .exe file and not force them to include the .config file change.
Is there a way to include this configuration option in my executable so I only provide the customer with a single file?
Thanks

Get a FREE t-shirt when you ask your first question.
We believe in human intelligence. Our moderation policy strictly prohibits the use of LLM content in our Q&A threads.
Hi
i think u cant do that programmatically... Not sure
i think u cant do that programmatically... Not sure
Ok... I'll give you the points and hope that someone has an idea of how to set the option within the executable.
Thanks for your help!
Thanks for your help!
.NET Programming
--
Questions
--
Followers
Top Experts
The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.