• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6198
  • Last Modified:

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:

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

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 = ""
        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"
        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
        'Catch ex As Exception
        'Return ex.Message
        'End Try
        Return returnValue
    End Function

Open in new window

  • 2
1 Solution
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
edenmachineAuthor Commented:
Yes, I've tried that as well.  I get the same error when I do it that way as well.
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.
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.

Join & Write a Comment

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now