0

I want to Pass XML string in HttpWebRequest Header? my application in MVC4 .Net Framework 4.5 C#. My code as below:

try
{
    String requestXML = @"<REPORT>      
                            <USER-DETAIL>
                                <NAME>ABC</NAME1>
                                <PASSWORD>12345</NAME2> 
                            </USER-DETAIL>
                        </REPORT>";

    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.demoservice.com/service/getReport");
    request.Accept = "application/xml";
    request.Method = "POST";
    request.Headers.Add("userId", "admin");
    request.Headers.Add("password", "***");
    request.Headers.Add("requestXml", requestXML);

    HttpWebResponse response;
    response = (HttpWebResponse)request.GetResponse();
    if (response.StatusCode == HttpStatusCode.OK)
    {
        Stream responseStream = response.GetResponseStream();
        String output = new StreamReader(responseStream).ReadToEnd();
        Console.WriteLine(output);
    }
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
    throw;
}

But I'm getting the following error,

Specified value has invalid CRLF characters. Parameter name: value

Please help, thanks in advance.

3
  • 1
    Did you try removing those line breaks and formatting xml into single line? Commented Sep 11, 2017 at 10:49
  • Why are you adding POST data in headers? What made you think you need to do that? Commented Sep 11, 2017 at 10:58
  • I solved my problem after remove all new lines and make a single line of xml. thanks to @shivaGopal Commented Sep 12, 2017 at 5:59

1 Answer 1

1

Do not add arbitrary data to the request headers. POST data should go in the request body. Request headers must conform to the HTTP spec, which the data you're erroneously trying to stuff in there doesn't.

This is how you should be doing it:

var data = Encoding.UTF8.GetBytes(requestXML);
request.ContentType = "application/xml";
request.ContentLength = data.Length;
using (var body = request.GetRequestStream())
{
    body.Write(data, 0, data.Length);
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the help @ChrisPratt, but API is provide by third party and required all parameters in a header segment of webRequest for the security reason. so i must have to pass all the parameters in a header.
Well, you could try to do something like base64 encode the data, but the third party would have to decode it on their end, so that may not be possible either. Long and short, the HTTP spec is being violated, so your simply hitting a wall of what's possible. You're either going to have to tell the third party to design a proper API or find a different third party.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.