Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.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# 如何用C发布非标准XML#_C#_Xml - Fatal编程技术网

C# 如何用C发布非标准XML#

C# 如何用C发布非标准XML#,c#,xml,C#,Xml,我有发布标准XML的实用程序,但我面临着与服务器交互的问题,这需要一些我不熟悉的东西 预期的XML格式如下所示: "xmldata=<txn><element_1>element_1_value</element_1><element_2>element_2_value</element_2></txn>" 我收到一条关于XML格式不正确的错误消息 当我使用我发现的一些东西时: var request = _request

我有发布标准XML的实用程序,但我面临着与服务器交互的问题,这需要一些我不熟悉的东西

预期的XML格式如下所示:

"xmldata=<txn><element_1>element_1_value</element_1><element_2>element_2_value</element_2></txn>"
我收到一条关于XML格式不正确的错误消息

当我使用我发现的一些东西时:

var request = _requestFactory.CreateCreditCardSaleRequest(xmlStringFromAbove);
WebRequest webRequest = WebRequest.Create("https://domain.com/process_some_xml.do");
webRequest.Method = "POST";

byte[] byteArray = Encoding.UTF8.GetBytes(request);
// Set the ContentType property of the WebRequest.
webRequest.ContentType = "text/xml; encoding='utf-8'";
// Set the ContentLength property of the WebRequest.
webRequest.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = webRequest.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 = webRequest.GetResponse();
// Display the status.
var httpWebResponse = (HttpWebResponse) response;
Console.WriteLine(httpWebResponse.StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
响应似乎表示成功,但没有预期的响应内容

我怀疑这与请求开头的“xmldata=”有关,但不能确定


有什么建议吗?

xmldata=
判断,似乎API希望数据以
格式URLCoded
发送。我建议试试这个:

string dataStr = "xmldata=" + HttpUtility.UrlEncode(XDocumentToString(xml));
byte[] data = Encoding.ASCII.GetBytes(dataStr);
这是:

client.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");

将内容类型更改为“text/plain”有什么不标准之处。@tallseth您发现了什么问题?这对解决OP的问题有什么帮助。非标准==不是我习惯的:)text/plain==相同的结果。
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");