Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在.NET中,对发布到URL的XML字符串进行编码的正确方法是什么?_C#_.net_Xml - Fatal编程技术网

C# 在.NET中,对发布到URL的XML字符串进行编码的正确方法是什么?

C# 在.NET中,对发布到URL的XML字符串进行编码的正确方法是什么?,c#,.net,xml,C#,.net,Xml,这是我第一次不得不通过将XML数据集发布到URL来使用供应商的API。如果使用对数据集进行编码,并手动将URL和数据集粘贴到浏览器中,则会得到成功的响应 但是,当尝试对.NET执行相同操作时,我从供应商API得到一个响应,指示我的数据集无效 以下是我从good ol’interweb上的其他地方复制的代码: public void PostXML(string value1, string value2, string value3, out string responseStatus,

这是我第一次不得不通过将XML数据集发布到URL来使用供应商的API。如果使用对数据集进行编码,并手动将URL和数据集粘贴到浏览器中,则会得到成功的响应

但是,当尝试对.NET执行相同操作时,我从供应商API得到一个响应,指示我的数据集无效

以下是我从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 containing 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();
    }

请记住,我得到了供应商API的响应。它只是告诉我我的数据集是无效的。我已经联系了供应商的客户支持。这已经证实了我的数据集本身是正确的,但在某种程度上我对它进行了错误的编码,以便供应商API能够识别它。

缺少一些东西

您可能应该从xml声明开始

<?xml version="1.0"?>

最后,请与供应商联系,向您发送示例代码/API…

缺少一些内容

您可能应该从xml声明开始

<?xml version="1.0"?>

最后,请与供应商联系,向您发送示例代码/API…

在调用Encoding.UTF8.GetBytespostData;之前,请尝试在post数据上使用System.Web.HttpUtility.UrlEncode

在调用Encoding.UTF8.GetBytespostData;之前,请尝试在post数据上使用System.Web.HttpUtility.UrlEncode

@user:根本就没有C.NET这个东西。语言为C语言;框架是.NET。当您说手动将URL和数据集粘贴到浏览器中时,您的意思是将URL和数据集一起粘贴到地址栏中吗?如果是这样,那么我们说的是HTTP GET而不是HTTP POST。@kbrimington:是的,这就是我的意思。@user:没有C.NET这样的东西。语言为C语言;框架是.NET。当您说手动将URL和数据集粘贴到浏览器中时,您的意思是将URL和数据集一起粘贴到地址栏中吗?如果是这样,那么我们说的是HTTPGET而不是HTTPPOST。@kbrimington:是的,这就是我的意思。
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();
}