Click here to Skip to main content
15,883,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
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
Maideen

Below is my code

What I have tried:

    Dim url = "https://gold.mxpress2u.net/gold/API.asmx"
    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
        streamWriter.Write(data)
    End If
    Dim httpResponse = CType(httpRequest.GetResponse(), HttpWebResponse)
    Dim streamReader = New StreamReader(httpResponse.GetResponseStream())
    If True Then
        Dim result = streamReader.ReadToEnd()
    End If
    Console.WriteLine(httpResponse.StatusCode)


XML
<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>
Posted
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
 
Comments
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:
XML
<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
 
Comments
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