1
我想实现一个支付网关(INTUIT支付网关)。我想序列化一个XML到模型类并保存到我的数据库。我使用桌面模型直观支付网关,因为托管模型是一个特别是SSL证书工作的痛苦,所以我不想尝试。XML文件模型映射asp.net MVC3(动态地将一个xml文件映射到一个模型类asp.net mvc3)
我必须提及,我能够使用下面提到的代码获得响应,在那里我卡住的是序列化xml并将响应保存到数据库中。这个XML是从我的项目中位于我的xmlfiles文件夹中的文件夹中提取的。
有关示例xml格式,请查看这一个。
这是一个XML文件,我试图张贴到URL(https://merchantaccount.ptc.quickbooks.com/j/AppGateway)
<?xml version="1.0"?>
<?qbmsxml version="4.5"?>
<QBMSXML>
<SignonMsgsRq>
<SignonDesktopRq>
<ClientDateTime>2012-07-25T17:13:45</ClientDateTime>
<ApplicationLogin>abc.abc.us</ApplicationLogin>
<ConnectionTicket>TGT-1-g42FGaMfOTQ82GcWFBpsuQ</ConnectionTicket>
</SignonDesktopRq>
</SignonMsgsRq>
<QBMSXMLMsgsRq>
<CustomerCreditCardChargeRq>
<TransRequestID>4540453787200</TransRequestID>
<CreditCardNumber>4111111111111111</CreditCardNumber>
<ExpirationMonth>12</ExpirationMonth>
<ExpirationYear>2016</ExpirationYear>
<IsCardPresent>false</IsCardPresent>
<Amount>10.00</Amount>
</CustomerCreditCardChargeRq>
</QBMSXMLMsgsRq>
</QBMSXML>
我用它来发表的帖子的网址
public ActionResult Index()
{
WebRequest req = null;
WebResponse rsp = null;
string fileName = Server.MapPath("~/XMLData/XMLFile1.xml");
string uri = "https://merchantaccount.ptc.quickbooks.com/j/AppGateway";
req = WebRequest.Create(uri);
//req.Proxy = WebProxy.GetDefaultProxy(); // Enable if using proxy
req.Method = "POST"; // Post method
req.ContentType = "application/x-qbmsxml"; // content type
// Wrap the request stream with a text-based writer
StreamWriter writer = new StreamWriter(req.GetRequestStream());
// Write the XML text into the stream
writer.WriteLine(this.GetTextFromXMLFile(fileName));
writer.Close();
// Send the data to the webserver
rsp = req.GetResponse();
// var resp = (Object)rsp.GetResponseStream();
if (rsp != null)
{
StreamReader inStream = new StreamReader(rsp.GetResponseStream());
var data = inStream.ReadToEnd();
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(data);
string path = Server.MapPath("~/XMLData/SampleXmlE2E.xml");
xmlDoc.Save(path);//regenerates the xml file in different system.
}
return View();
//XElement root = new XElement("root");
//root.Add(new XElement("element1"));
//root.Add(new XElement("element2"));
//root.Add(new XAttribute("attribute1", "a value"));
//return new XmlResult(root);
}
private string GetTextFromXMLFile(string file)
{
StreamReader reader = new StreamReader(file);
string ret = reader.ReadToEnd();
reader.Close();
return ret;
}
private string SendRequest(Uri UriObj, string data)
{
string _result;
var request = (HttpWebRequest)WebRequest.Create(UriObj);
request.Method = "POST";
request.ContentType = "text/xml";
var writer = new StreamWriter(request.GetRequestStream());
writer.Write(data);
writer.Close();
var response = (HttpWebResponse)request.GetResponse();
var streamResponse = response.GetResponseStream();
var streamRead = new StreamReader(streamResponse);
_result = streamRead.ReadToEnd().Trim();
streamRead.Close();
streamResponse.Close();
response.Close();
return _result;
}
控制器的任何帮助将不胜感激。由于
它运行但始终返回空,我已确保文件名被提取,并且它加载xml。 – 2012-08-01 10:08:33
public ViewResult Index() var yourXmlFilePath = Server.MapPath(“〜/ XMLData/XMLFile1.xml”); var deserializedClass = DeSerialize(yourXmlFilePath); return View(); } –
2012-08-01 10:09:36
varun,在xml DeSerialize方法中,在使用块内发生了什么?另外,请尝试打开一个简单的控制台应用程序,并从我的答案中复制代码,并查看发生了什么。最后,你确实意识到你没有将任何东西传递给你的索引视图。你的返回值应该是'return View(deserializedClass);'并且你的Index.cshtml应该有一个定义为QbmsXml类型的模型,即'@model yournamespace.Models.QbmsXml' – 2012-08-01 10:22:27