我的要求是将我的ASP.NET应用程序数据插入计数器。 [英] My requirement is to insert my ASP.NET application data into tally.

查看:56
本文介绍了我的要求是将我的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

推荐答案

SysName:XML< / SVEXPORTFORMAT>< PRODUCTTYPE>所有库存大师< / PRODUCTTTYPE>< / STATICVARIABLES>< / REQUESTDESC> < / EXPORTDATA>< / BODY>< / ENVELOPE>;
TallyRequest = WebRequest.Create( http:// localhost:9000);
((HttpWebRequest)TallyRequest).UserAgent = .NET Framework示例客户端;
TallyRequest.Method = 发布;
字符串 postData = RequestXML;
byte [] byteArray = System.Text.Encoding.UTF8.GetBytes(postData);
TallyRequest.ContentType = 应用程序/ x WWW的形式进行了urlencoded;
TallyRequest.ContentLength = byteArray.Length;
流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(< span class =code-keyword> object
sender,EventArgs e)
{
try
{
ConnectToTally();
string xmlstc = string .Empty;
xmlstc = xmlstc + < MultipleStockItems = + \ + 所有项目 + \ACTION = + \ + 创建 + \>;
xmlstc = < ENVELOPE>;
xmlstc = xmlstc + < HEADER>;
xmlstc = xmlstc + < TALLYREQUEST>导出数据< / 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 = + \ + 所有项目 + \ACTION = + \ + 创建 + \>;
xmlstc = xmlstc + < NameOfItem> + item + < / NameOfItem>;
xmlstc = xmlstc + < under> + group + < / under>;
xmlstc = xmlstc + < units> + unit + < / units>;
xmlstc = xmlstc + < OpeningQuantity> +数量+ < / 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:/ /本地主机: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( 数据插入Tally成功);
streamWriter.Close();
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


不要使用字符串连接来构建XML文档。如果您的用户输入了意外数据,他们可以将任意XML注入您的文档。使用 LINQ to XML [ ^ ]。



您的XML文档无效 - 您可以不直接为元素名称指定属性值:

Don't use string concatenation to build an XML document. If your users enter unexpected data, they could inject arbitrary XML into your document. Use something like LINQ to XML[^] instead.

Your XML document is invalid - you can't assign an attribute value directly to an element name:
<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应用程序数据插入计数器。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆