ERROR: This header must be modified using the appropriate property. Parameter name: name

"This header must be modified using the appropriate property. Parameter name: name"

I get the error above when trying to pass the headers from one XML over HTTP post to another.  I've attached the error screenshot as well as my code.  I works fine on the response - but just not on the request.  How can I accomplish this?  The basic purpose of this code is to receive an XML over HTTP post from the first server, read the XML, store it into a database and then repost the EXACT same XML (headers included) to a third server so that the third server gets the exact same XML post that the first server sent.

Current Process:
SERVER1 ===XML===> SERVER3

New Process:
SERVER1 ===XML===> SERVER2 (store XML data) ===XML===> SERVER3

TIA!
Public Function PostXml(ByVal url As String, ByVal xml As String, ByVal headers As NameValueCollection, ByRef ReturnHeaders As NameValueCollection, ByRef ReturnStatusCode As Integer) As String
        Dim returnValue As String = ""
        'Try
        
        Dim bytes As Byte() = Encoding.UTF8.GetBytes(xml)
        Dim request As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
        request.Method = "POST"
        request.ContentLength = bytes.Length
        request.ContentType = "text/xml"
        
        request.Headers.Add(headers)
        
        Call WriteToFile(SendFile & "Headers.txt", request.Headers.ToString())
        
        Using requestStream As Stream = request.GetRequestStream()
            requestStream.Write(bytes, 0, bytes.Length)
        End Using
        
        Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
            
        If response.StatusCode <> HttpStatusCode.OK Then
            Dim message As String = [String].Format("POST failed. Received HTTP {0}", response.StatusCode)
            Throw New ApplicationException(message)
        End If
        Dim rs As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
        Dim receiveStream As Stream = rs.GetResponseStream()
        Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")
        Dim readStream As New StreamReader(receiveStream, encode)
            
        Dim read(256) As [Char]
        Dim count As Integer = readStream.Read(read, 0, 256)
        While count > 0
            Dim str As New [String](read, 0, count)
            returnValue += str
            count = readStream.Read(read, 0, 256)
        End While
        ReturnStatusCode = rs.StatusCode
        ReturnHeaders = rs.Headers
        readStream.Close()
        rs.Close()
            
        'Catch ex As Exception
        'Return ex.Message
        'End Try
        Return returnValue
    End Function

Open in new window

Capture1.JPG
LVL 1
edenmachineAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AmarIs26Commented:
You could try adding the headers in a loop (note the code below is just pseudo code)
For each item in headers
request.headers(item.key) = item.value
next item
0
edenmachineAuthor Commented:
Yes, I've tried that as well.  I get the same error when I do it that way as well.
0
edenmachineAuthor Commented:
Hmm - apparently you can't just take the old headers and through them into the new heards (I assume because some are read-only) so once I just added the ones I knew I needed, it worked fine.
request.Headers.Add("Cookie",headers.Item("Cookie"))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.