本文介绍了我的要求是将我的ASP.NET应用程序数据插入计数器。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 public partial class WebForm1 : System.Web.UI.Page { private static string RequestXML; private static WebRequest TallyRequest; protected void Page_Load(object sender, EventArgs e) { } public static DataSet ConnectToTally() { //try //{ RequestXML = "<ENVELOPE><HEADER><TALLYREQUEST>Export Data</TALLYREQUEST></HEADER><BODY><EXPORTDATA><REQUESTDESC><REPORTNAME>List of Products</REPORTNAME><STATICVARIABLES><SVEXPORTFORMAT>$$SysName:XML</SVEXPORTFORMAT><PRODUCTTYPE>All Inventory Masters</PRODUCTTTYPE></STATICVARIABLES></REQUESTDESC></EXPORTDATA></BODY></ENVELOPE>"; TallyRequest = WebRequest.Create("http://localhost:9000"); ((HttpWebRequest)TallyRequest).UserAgent = ".NET Framework Example Client"; TallyRequest.Method = "Post"; string postData = RequestXML; byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(postData); TallyRequest.ContentType = "application/x-www-form-urlencoded"; TallyRequest.ContentLength = byteArray.Length; Stream dataStream = TallyRequest.GetRequestStream(); dataStream.Write(byteArray, 0, byteArray.Length); dataStream.Close(); WebResponse response = TallyRequest.GetResponse(); string Response = (((HttpWebResponse)response).StatusDescription).ToString(); dataStream = response.GetResponseStream(); StreamReader reader = new StreamReader(dataStream); string responseFromTallyServer = reader.ReadToEnd().ToString(); DataSet TallyResponseDataSet = new DataSet(); TallyResponseDataSet.ReadXml(new StringReader(responseFromTallyServer)); reader.Close(); dataStream.Close(); response.Close(); byteArray = null; response = null; responseFromTallyServer = null; Response = null; dataStream = null; return TallyResponseDataSet; } protected void btn_Click(object sender, EventArgs e) { try { ConnectToTally(); string xmlstc = string.Empty; xmlstc = xmlstc + "<MultipleStockItems=" + "\"" + "All items" + "\" ACTION=" + "\"" + "Create" + "\">"; xmlstc = "<ENVELOPE>"; xmlstc = xmlstc + "<HEADER>"; xmlstc = xmlstc + "<TALLYREQUEST>Export Data</TALLYREQUEST>"; xmlstc = xmlstc + "</HEADER>"; xmlstc = xmlstc + "<BODY>"; xmlstc = xmlstc + "<IMPORTDATA>"; xmlstc = xmlstc + "<REQUESTDESC>"; xmlstc = xmlstc + "<REPORTNAME>ListOfProducts</REPORTNAME>"; xmlstc = xmlstc + "<STATICVARIABLES>"; xmlstc = xmlstc + "<SVCURRENTCOMPANY>Sphinx</SVCURRENTCOMPANY>"; xmlstc = xmlstc + "</STATICVARIABLES>"; xmlstc = xmlstc + "</REQUESTDESC>"; xmlstc = xmlstc + "<REQUESTDATA>"; string item= itm.Text; string group = grp.Text; string unit = unt.Text; string quantity = qnt.Text; string Rate = rate.Text; string msi = ""; xmlstc = xmlstc + "<TALLYMESSAGE >"; xmlstc = xmlstc + "<MultipleStockItems=" + "\"" + "All items" + "\" ACTION=" + "\"" + "Create" + "\">"; xmlstc = xmlstc + "<NameOfItem>" + item + "</NameOfItem>"; xmlstc = xmlstc + "<under>" + group + "</under>"; xmlstc = xmlstc + "<units>" + unit + "</units>"; xmlstc = xmlstc + "<OpeningQuantity>" + quantity + "</OpeningQuantity>"; xmlstc = xmlstc + "<Rate>" + rate + "</Rate>"; xmlstc = xmlstc + "<MultipleStockItems>" + msi + "</MultipleStockItems>"; xmlstc = xmlstc + "</MultipleStockItems>"; xmlstc = xmlstc + "</TALLYMESSAGE>"; xmlstc = xmlstc + "</REQUESTDATA>"; xmlstc = xmlstc + "</IMPORTDATA>"; xmlstc = xmlstc + "</BODY>"; xmlstc = xmlstc + "</ENVELOPE>"; string result = ""; HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:9000"); httpWebRequest.Method = "POST"; httpWebRequest.ContentLength = xmlstc.Length; httpWebRequest.ContentType = "application/x-www-form-urlencoded"; StreamWriter streamWriter; streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()); streamWriter.Write(xmlstc); Response.Write("Data inserted into Tally sucessfully"); streamWriter.Close(); 我尝试过: 我试过上面的代码。但我无法将我的文本框数据存储到计数器中。请帮助我What I have tried:I have tried the above code. But i am unable to store my textbox datas into tally. Please help me in this推荐答案 我尝试过: 我试过上面的代码。但我无法将我的文本框数据存储到计数器中。请帮我这个What I have tried:I have tried the above code. But i am unable to store my textbox datas into tally. Please help me in this <MultipleStockItems="All items" ACTION="Create"> 要成为有效的XML,您需要一个属性名称: To be valid XML, you would need an attribute name here:<MultipleStockItems SomeAttribute="All items" ACTION="Create"> 您还有第二个 MultipleStockItems 元素嵌套在外部 MultipleStockItems 元素,其内容为空字符串。这对我来说不合适 - 你需要检查XML应该是什么样子。 您可能需要使用正确的内容在您的Web请求上键入 - XML字符串不是application / x-www-form-urlencoded。您需要检查接收数据的代码,以查看它所期望的内容类型。 您需要调用 httpWebRequest .GetResponse()实际将请求发送到服务器。 You've also got a second MultipleStockItems element nested under the outer MultipleStockItems element, with an empty string as its content. That doesn't look right to me - you'll need to check what the XML is supposed to look like.You'll probably need to use the correct content type on your web request - an XML string is not "application/x-www-form-urlencoded". You'll need to check the code that receives the data to see what content type it's expecting.And you'll need to call httpWebRequest.GetResponse() to actually send the request to the server.XDocument xml = new XDocument( new XElement("ENVELOPE", new XElement("HEADER", new XElement("TALLYREQUEST", "Export Data") ), new XElement("BODY", new XElement("IMPORTDATA", new XElement("REQUESTDESC", new XElement("REPORTNAME", "ListOfProducts"), new XElement("STATICVARIABLES", new XElement("SVCURRENTCOMPANY", "Sphinx") ) // STATICVARIABLES ), // REQUESTDESC new XElement("REQUESTDATA", new XElement("TALLYMESSAGE", new XElement("MultipleStockItems", new XAttribute("SomeAttribute", "All items"), new XAttribute("ACTION", "Create"), new XElement("NameOfItem", itm.Text), new XElement("under", grp.Text), new XElement("units", unt.Text), new XElement("OpeningQuantity", qnt.Text), new XElement("Rate", rate.Text), // This one doesn't look right: new XElement("MultipleStockItems", string.Empty) ) // MultipleStockItems )// TALLYMESSAGE ) // REQUESTDATA ) // IMPORTDATA ) // BODY ) // ENVELOPE);HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create("http://localhost:9000");httpWebRequest.Method = "POST";// TODO: Use the correct content type:httpWebRequest.ContentType = "application/x-www-form-urlencoded";using (Stream requestStream = httpWebRequest.GetRequestStream()){ xml.Save(requestStream, SaveOptions.DisableFormatting);}// Need to actually send the request:using (WebResponse response = httpWebRequest.GetResponse()){}Response.Write("Data inserted into Tally sucessfully"); 这篇关于我的要求是将我的ASP.NET应用程序数据插入计数器。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-29 09:35