0

I'm working on a project where I have to send product information via HTTP POST in XML string to a web server. It turns out that certain product names may have a % sign in their name, such as ".05% Topical Cream". Whenever I attempt to send XML data that contained a % sign in the product name, I get an error apparently because when encoding the XML string data the percent sign caused the data to become malformed.

How can I encode and send the XML string data with % sign in product name safely?

XML Data:

<node>
      <product>
        <BrandName>amlodipine besylate (bulk) 100 % Powder</BrandName>
      </product>
  </node>

Web request code:

public string MakeWebServerRequest(string url, string data)
    {
        var parms = System.Web.HttpUtility.UrlEncode(data);
        byte[] bytes = Encoding.UTF8.GetBytes("xml=" + parms);
        string webResponse = String.Empty;
        try
        {
            System.Web.HttpUtility.UrlEncode(data);
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = bytes.Length;

            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.WriteTimeout = 3000;
                reqStream.Write(bytes, 0, bytes.Length);
                reqStream.Close();
            }
            using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
            {
                using (StreamReader rdr = new StreamReader(response.GetResponseStream()))
                {
                    webResponse = rdr.ReadToEnd();
                    rdr.Close();
                }
                response.Close();
            }
        }

Should I be creating the web request differently? What can I do to resolve, while maintaining the product name?

Corrected - and working now. Thanks

Thanks

Encryption
  • 1,809
  • 9
  • 37
  • 52
  • 1
    Please show input that causes the error ... And do you really have to construct XML with string contatenation? :( . – Alexei Levenkov Dec 28 '12 at 20:19
  • I've added all the XML I can show without giving away sensitive info...hope its enough. I dont construct the XML string via concat, I smply add the first line to the string. I use XDocument and have a base XML that I fill in fields with data – Encryption Dec 28 '12 at 20:23

1 Answers1

1

You need to construct request propely. application/x-www-form-urlencoded means that each parameter is Url-encoded. In your case xml parameter must have value correctly encoded, not just blindly concatenated. Below is sample that should give you stared... hopefully you'll be able to avoid string concateneation to construct XML (and insane way of constructing string constant with queotes you have in original code):

var parameterValue = System.Web.HttpUtility.UrlEncode("<xml>" + data);
byte[] bytes = Encoding.UTF8.GetBytes("xml=" + parameterValue);

There are also plenty of samples how to correctly construct requests of this kind. I.e. C# web request with POST encoding question

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179