Click here to Skip to main content
15,880,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an issue in POST XML data to External API URL
I got Error is System.ArgumentException: 'Specified value has invalid CRLF characters.
Please advice me
Thank you

Below is my code

What I have tried:

    Dim url = ""
    Dim httpRequest = CType(WebRequest.Create(url), HttpWebRequest)
    httpRequest.Method = "POST"
    httpRequest.ContentType = "application/xml"
    httpRequest.Accept = "application/xml"

    Dim data = Me.txtXML.Text
    httpRequest.Headers("strAPIKey") = "API Key"
    httpRequest.Headers("strCustomerAccount") = "Account No"
    httpRequest.Headers("intPrintFormatType") = "1"
    httpRequest.Headers("strShipmentDetailsXML") = data

'' After debug the error is on this "httpRequest.Headers("strShipmentDetailsXML") = data"
'' Error is System.ArgumentException: 'Specified value has invalid CRLF characters.

    Dim streamWriter = New StreamWriter(httpRequest.GetRequestStream())
    If True Then
    End If
    Dim httpResponse = CType(httpRequest.GetResponse(), HttpWebResponse)
    Dim streamReader = New StreamReader(httpResponse.GetResponseStream())
    If True Then
        Dim result = streamReader.ReadToEnd()
    End If

<xml><ServiceTypeID>1</ServiceTypeID><ShipperContactPerson>padhma</ShipperContactPerson><ShipperCompanyName>Jetpharma Sdn Bhd</ShipperCompanyName><ShipperPhoneNumber>03-8076 1651</ShipperPhoneNumber><OriginCountryID>1</OriginCountryID><OriginStateID>12</OriginStateID><OriginPostcode>47000</OriginPostcode><OriginCity>Bandar Puchong Jaya</OriginCity><OriginAddress>1, 11 & 13, Jalan Rajawali 2, Bandar Puchong Jaya, 
47100 Puchong, Selangor</OriginAddress><RecpContactPerson>contact Person</RecpContactPerson><RecpCompanyName>Azam Bekal Enterprise</RecpCompanyName><RecpPhoneNumber>0146675956</RecpPhoneNumber><DestCountryID>1</DestCountryID><DestStateID>11</DestStateID><DestPostcode>45000</DestPostcode><DestCity>Ipoh</DestCity><DestAddress>Farmasi Azam No. 6, Jalan Cindai Jaya 1, Taman Cindai Jaya, 08000 Sungai Petani, Kedah. 08000</DestAddress><Description>small package</Description><Remarks>Remarks</Remarks><Pieces>6</Pieces><Weight>0.00</Weight><Length>1.00</Length><Width>1.00</Width><Height>1.00</Height><CODAmount>0.00</CODAmount><ReferenceNumber>SO-1298712</ReferenceNumber></xml>
Updated 24-Dec-22 21:26pm

This generally happens when you have line breaks and tab spaces in your data being passed as HttpRequest header.

Remove all linebreaks and spaces from strShipmentDetailsXML and try. Not sure but seems there is line break or space post OrininStateID tag above.

Similar issue reported here: ASP.Net Error: Specified value has invalid CRLF characters[^]
Share this answer
Maideen Abdul Kader 24-Dec-22 23:31pm    
Mr.Sandeep. thank you very much for prompt response. There is no Line breaks or space in XML Format I have already posted.
Pls advice me.
It is quite likely that the problem is with the line:
<OriginAddress>1, 11 & 13, Jalan Rajawali 2, Bandar Puchong Jaya, 

The ampersand character between the 11 and 13 has (I suspect) special meaning to XML, and should be escaped. Check Google for suggested solutions.
Share this answer
0x01AA 25-Dec-22 4:17am    
Eagle eye, a 5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900