Http request to tally server

2019-05-31 10:23发布

This is the code that I am using and output I am receiving.

Output:
� ��.Yk{�1�<�";����������B�Ze�z ������"�脙��w�3�嵪���5���V�X5��OIdY���/�$���Z�d��%� ����}}����;r?(ۄ�?xS#%�Da���$�dʩ�V��g2_F���.�����/��

In this Line: Stream requestStream = request.GetRequestStream();

requestStream.Length and requestStream.Position threw an exception of type System.NotSupportedException

public void Test() {   
     string xmlMessage =  "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +

         "<HEADER>" +  
             "<ID>Ram</ID>" + 
             "<SOURCE></SOURCE>" +       
             "<TARGET></TARGET>" +
             "<CONTENT-TYPE>text/xml;charset=utf-8</CONTENT-TYPE>" +                       
          "</HEADER>" +

          "<ENVELOPE>" +

               "<VERSION></VERSION>" +
               "<REQVERSION></REQVERSION>" +
               "<TALLYREQUEST></TALLYREQUEST>" +
               "<TYPE></TYPE>" +
               "<ID></ID>" + 
               "<CONTENT-TYPE>/xml;charset=utf-8</CONTENT-TYPE>" +
               "<SESSIONID></SESSIONID>" +                      
               "<TOKEN></TOKEN>" +

               "<BODY>" +
                   "<DESC>" +
                   "<STATICVARIABLES>" +
                   "<SVINCLUDE></SVINCLUDE>" +
                   "</STATICVARIABLES>" +
                   "</DESC>" +
               "</BODY>" +     
          "</ENVELOPE>";

      //Sending the request to Tally test URL address
        string url = "www.xxxxxxx.com";

        //create a request
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.KeepAlive = false;
        request.ProtocolVersion = HttpVersion.Version10;
        request.Method = "POST";
        string postData = xmlMessage;
        // turn our request string into a byte stream
        byte[] byteArray = Encoding.UTF8.GetBytes(postData);
        //byte[] requestInFormOfBytes = new UTF8Encoding(false).GetBytes(xmlMessage);

        request.ContentType = "text/xml;charset=utf-8";

        request.ContentLength = byteArray.Length;

        Stream requestStream = request.GetRequestStream();  

        requestStream.Write(byteArray, 0, byteArray.Length);

        requestStream.Close();

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();   

        StreamReader respStream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);

       string receivedResponse = respStream.ReadToEnd();
       Label2.Text = receivedResponse;

       respStream.Close();
       response.Close();  
    }

1条回答
爷、活的狠高调
2楼-- · 2019-05-31 11:08

Try this

public void Test() {

string xmlMessage =  "<ENVELOPE>
<HEADER>
    <VERSION>1</VERSION>
    <REQVERSION>1</REQVERSION>
    <TALLYREQUEST>EXPORT</TALLYREQUEST>
    <TYPE>DATA</TYPE>
    <ID>TPGETCOMPANIES</ID>
    <SESSIONID>" .$session. "</SESSIONID>
    <TOKEN>" .$token. "</TOKEN>
</HEADER>
<BODY>
<DESC>
<STATICVARIABLES>
    <SVINCLUDE>CONNECTED</SVINCLUDE>
</STATICVARIABLES>
</DESC>
</BODY>
</ENVELOPE>";

//Sending the request to Tally test URL address
string url = "www.xxxxxxx.com";

//create a request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.KeepAlive = false;
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
string postData = xmlMessage;

byte[] byteArray = Encoding.UTF8.GetBytes(postData);

request.ContentType = "text/xml;charset=utf-8";
request.ContentLength = byteArray.Length;

request.Headers.Add("ID", "TPGETCOMPANIES");
request.Headers.Add("SOURCE", "EA");
request.Headers.Add("TARGET", "TNS");
request.Headers.Add("CONTENT-TYPE", "text/xml;charset=utf-8");

Stream requestStream = request.GetRequestStream();  
requestStream.Write(byteArray, 0, byteArray.Length);
requestStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();   
StreamReader respStream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);

string receivedResponse = respStream.ReadToEnd();
Label2.Text = receivedResponse;

respStream.Close();
response.Close();  
}
查看更多
登录 后发表回答