How to solve this "Specified value has invalid CRLF characters"?

SILS 61 Reputation points
2022-12-25T03:35:08.873+00:00

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

        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 "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><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 &amp; 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>  
ASP.NET API
ASP.NET API
ASP.NET: A set of technologies in the .NET Framework for building web applications and XML web services.API: A software intermediary that allows two applications to interact with each other.
326 questions
0 comments No comments
{count} votes

Accepted answer
  1. QiYou-MSFT 4,321 Reputation points Microsoft Vendor
    2022-12-26T07:09:23.073+00:00

    Hi @SILS ,
    First of all, in terms of results: "Specified value has invalid CRLF characters". Usually HTTP is required when publishing parameters to APIs, so the problem is with your HTTP. If you have carriage returns in your HTTP, this will cause these problems. So the solution I gave is to look at and delete the CRLF symbols in your HTTP, and if you continue to report errors, I will continue to help you solve the problem.

    Best Regards
    Qi You


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.