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_DATA"

        Dim PostdataArray As Byte() = Encoding.ASCII.GetBytes(Postdata)

        ' 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.PreAuthenticate = True

            ' Associate the NetworkCrbedential object with the 'WebRequest' object
            myWebClient.Credentials = NetworkCredential

            ' Add required HTTP headers to request
            myWebClient.Headers.Add("Accept", "*/*")
            myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")

            ' 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!
jamesspoAsked:
Who is Participating?
 
udhayakumardConnect With a Mentor Commented:
This problem occurs when the .NET Framework detects that the server response does not comply with HTTP 1.1 RFC. This problem may occur when the response contains incorrect headers or incorrect header delimiters
0
 
udhayakumardCommented:
this link will be useful

http://support.microsoft.com/kb/915599
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
jamesspoAuthor Commented:
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="true" />
</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
0
 
jamesspoAuthor Commented:
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...
0
 
udhayakumardCommented:
U just place the script in ur customers server
0
 
jamesspoAuthor Commented:
Currently, the only method I know of to instruct vb to ignore the illegal header from the server is to add useUnsafeHeaderParsing="true" 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
0
 
udhayakumardCommented:
Hi

i think u cant do that programmatically... Not sure
0
 
jamesspoAuthor Commented:
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!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.