Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Using vb.net, how to send POST request with post data and save the binary response to a file?

Posted on 2007-09-09
9
Medium Priority
?
15,317 Views
Last Modified: 2008-01-09
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!
0
Comment
Question by:jamesspo
  • 5
  • 4
9 Comments
 
LVL 11

Accepted Solution

by:
udhayakumard earned 300 total points
ID: 19859135
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
 
LVL 11

Expert Comment

by:udhayakumard
ID: 19859151
this link will be useful

http://support.microsoft.com/kb/915599
0
 
LVL 11

Expert Comment

by:udhayakumard
ID: 19859173
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 

Author Comment

by:jamesspo
ID: 19862435
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
 

Author Comment

by:jamesspo
ID: 19862464
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
 
LVL 11

Expert Comment

by:udhayakumard
ID: 19862664
U just place the script in ur customers server
0
 

Author Comment

by:jamesspo
ID: 19863591
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
 
LVL 11

Expert Comment

by:udhayakumard
ID: 19863637
Hi

i think u cant do that programmatically... Not sure
0
 

Author Comment

by:jamesspo
ID: 19863795
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

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question