开发者

What is the correct way in .NET to encode an XML string for Posting to a URL?

For the first time, I am having to use a vendor's API by posting an XML dataset to a URL. If I encode the dataset using http://meyerweb.com/eric/tools/dencoder/, and manually paste the URL & dataset into a browser, I get a successful response.

However, when trying to do the same thing with .NET, I get a response from the vendor API indicating that my dataset is invalid.

Here's my code (which I copied from elsewhere on the good ol' interweb):

    public void PostXML(string value1, string value2, string value3, out string responseStatus, out string responseBody)
    {
        // Create a request using a URL that can receive a post. 
        WebRequest request = WebRequest.Create("https://www.vendorURL.com/API/page.html?input=");

        // Set the Method property of the request to POST.
        request.Method = "POST";

        // Create POST data and convert it to a byte array.
        string postData = "<DATASET>";
        postData += "<FIELD1>" + value1+ "</FIELD1>";
        postData += "<FIELD2>" + value2 + "</FIELD2>";
        postData += "<FIELD3>" + value3 + "</FIELD3>";
        postData += "</DATASET>";

        byte[] byteArray = Encoding.UTF8.GetBytes(postData);

        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";

        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;

        // Get the request stream.
        Stream dataStream = request.GetRequestStream();

        // Write the data to the request stream.
        dataStream.Write(byteArray, 0, byteArray.Length);

        // Close the Stream object.
        dataStream.Close();

        // Get the response.
        WebResponse response = request.GetResponse();

        // Assign the status.
        responseStatus = ((HttpWebResponse)response).StatusDescription;

        // Get the stream c开发者_高级运维ontaining content returned by the server.
        dataStream = response.GetResponseStream();

        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader(dataStream);

        // Read the content.
        responseBody = reader.ReadToEnd();

        // Clean up the streams.
        reader.Close();
        dataStream.Close();
        response.Close();
    }

Keep in mind, I am getting a response from the vendor API. It's just telling me that my dataset is invalid. I have already contacted the vendor's customer support. That have confirmed that my dataset itself is correct, but somewhere along the way I'm encoding it incorrectly for it to be recognizable by the vendor API.


Several things are missing.

You should probably start with an xml declaration

<?xml version="1.0"?>

Also are you sure the dataset tags are upper case? Case is significant in Xml.

<?xml version="1.0"?>
<dataset><field>value</field></dataset>

Additionally you might try XmlWriter:

using(var writer = XmlWriter.Create(stream))
{
  writer.WriteStartDocument();
  writer.WriteStartElement("dataset");
  writer.WriteElementString("field1", "value");
  writer.WriteElementString("field2", "value");
  writer.WriteElementString("field3", "value");
  writer.WriteEndElement();
  writer.WriteEndDocument();
}

Finally contact the vendor to send you sample code/API...


Try using System.Web.HttpUtility.UrlEncode on your post data before invoking "Encoding.UTF8.GetBytes(postData);".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜