我正在尝试将XML字符串发布到我的计算机上运行的本地网站。 我的XML是从以下c#生成的:
public string CreateOrderToSend(CreateOrderModel OrderModel, byte[] data, string username, string password, string searchPack)
{
// new string for storing orderPlacement xml instructions
string xml_OP = "";
// start populating with data (maybe from params?)
using (TextWriter strWriter = new Utf8StringWriter())
{
using (XmlTextWriter xmlWriter = new XmlTextWriter(strWriter))
{
// not sure if this is needed
xmlWriter.WriteStartDocument(false);
xmlWriter.WriteWhitespace("\n");
xmlWriter.WriteStartElement("OrderPlacement");
xmlWriter.WriteAttributeString("xmlns", "hidden");
xmlWriter.WriteWhitespace("\n\n");
xmlWriter.WriteStartElement("Authentication");
xmlWriter.WriteAttributeString("xmlns", "hidden");
xmlWriter.WriteWhitespace("\n");
xmlWriter.WriteElementString("Username", username);
xmlWriter.WriteWhitespace("\n");
xmlWriter.WriteElementString("Password", password);
xmlWriter.WriteWhitespace("\n");
xmlWriter.WriteEndElement(); // Authentication
xmlWriter.WriteWhitespace("\n\n");
xmlWriter.WriteElementString("FileReference", OrderModel.FileRef); // FileReference
xmlWriter.WriteWhitespace("\n\n");
xmlWriter.WriteStartElement("Address");
xmlWriter.WriteWhitespace("\n");
xmlWriter.WriteElementString("PropertyIdentifier", OrderModel.Address1);
xmlWriter.WriteWhitespace("\n");
xmlWriter.WriteElementString("Street", OrderModel.Address2);
xmlWriter.WriteWhitespace("\n");
xmlWriter.WriteElementString("Town", OrderModel.Address3);
xmlWriter.WriteWhitespace("\n");
xmlWriter.WriteElementString("Postcode", OrderModel.Address4);
xmlWriter.WriteWhitespace("\n");
xmlWriter.WriteEndElement(); // Address
xmlWriter.WriteWhitespace("\n\n");
xmlWriter.WriteElementString("UPRN", OrderModel.UPRN); // UPRN
xmlWriter.WriteWhitespace("\n\n");
xmlWriter.WriteElementString("NewBuild", OrderModel.NewBuild.ToString().ToLower()); // NewBuild
xmlWriter.WriteWhitespace("\n\n");
xmlWriter.WriteStartElement("SitePlanEmbeddedObject"); // (base64 - stream)
xmlWriter.WriteAttributeString("Name", "pdfName.pdf");
xmlWriter.WriteAttributeString("ContentType", "Application/Pdf");
xmlWriter.WriteString(data.ToString()); // base64 string
xmlWriter.WriteEndElement(); // SitePlan
xmlWriter.WriteWhitespace("\n\n");
xmlWriter.WriteStartElement("Pack");
xmlWriter.WriteWhitespace("\n");
xmlWriter.WriteElementString("PackCode", searchPack);
xmlWriter.WriteWhitespace("\n");
xmlWriter.WriteEndElement(); // Pack (foreach - can be more than one pack)
xmlWriter.WriteWhitespace("\n\n");
xmlWriter.WriteEndDocument(); // OrderPlacement
xmlWriter.Flush();
xml_OP = strWriter.ToString();
}
}
string newstring = xml_OP;
return newstring;
}
为了将数据移动到站点,我正在运行它(上面的xmlWriter方法填充了xmlContent):
postXMLData("http://localhost:41092/Home/Order/", xmlContent);
我的postXMLData方法看起来像这样。
public string postXMLData(string destination, string xmlContent)
{
for (int i = 0; i < NumberOfRetries; i++)
{
string byteValue = Convert.ToBase64String(Encoding.UTF8.GetBytes(xmlContent));
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destination);
byte[] bytes = Encoding.ASCII.GetBytes(xmlContent);
request.ContentType = "application/x-www-form-urlencoded";
//request.ContentType = "text/xml; encoding='utf-8'";
request.ContentLength = bytes.Length;
request.Method = "POST";
Stream req = request.GetRequestStream();
req.Write(bytes, 0, bytes.Length);
req.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream respStream = response.GetResponseStream();
string respString = new StreamReader(respStream).ReadToEnd();
{
string responseStr = new StreamReader(response.GetResponseStream()).ReadToEnd();
return responseStr;
}
return null;
}
catch (Exception ex)
{
if (i == NumberOfRetries)
{
return null;
}
}
}
return null;
}
我正在运行一个带有几乎为空控制器的简单mvc网站。
运行postXMLData方法时,在控制器中命中了一个断点,但传入XML的值为空。
将“ this”拖到我的监视窗口时,this.Request.Form.Allkeys [0]下的值等于"<?xml version"
。
public class HomeController : Controller
{
[HttpPost]
public ActionResult Order(string incomingXML)
{ // breakpoint is here
var content = this.Request.Form.AllKeys;
return null;
}
}
如果AllKeys字段中有内容,我必须非常接近,但似乎差一点了? 还是MVC网站通过xml的解决方案错误?也许是网络API? 请帮忙!
答案 0 :(得分:0)
更新:
奇怪的是,由于我在xml属性中出现了“ =”符号,因此XML无法正确解析。 我注释掉了这些,并将XML值成功存储在字段中:
this.Request.Form.AllKeys[0]
我可能不得不将属性值分别解析为XML,然后将其动态插入到我的MVC中。