我想与计数服务器通信,我发送此代码用于http请求和来自计数服务器的响应是:
ü ¦€.Yk{ê1±<á";…òò÷ÁŽ›¾±àBÃZe´z ú÷¨éß"íè„™ýÊwº3€åµª§µ¡5ÒÀýVÿX5¥OIdY©çÝ/Ì$ŠË¼Zœdí¼Ã%Ö Ýø®‘}}á–À†™;r?(Û„“?xS#%öDaÊʆ$“dÊ©V´ë†g2_FªÖ.·£Ð½†/ò
我的代码在下面给出
public void Test()
{
string xmlMessage = "<ENVELOPE><HEADER> <ID>TPGETCOMPANIES</ID> <SOURCE>EA</SOURCE> <TYPE>DATA</TYPE> <CONTENT-TYPE>text/xml;charset=utf-8</CONTENT-TYPE> <SESSIONID>1408730012927569997</SESSIONID> <TALLYREQUEST>Import</TALLYREQUEST> <TARGET>TNS</TARGET> </HEADER> <BODY> <DESC> <STATICVARIABLES> <SVINCLUDE>Connected</SVINCLUDE> </STATICVARIABLES></DESC></BODY></ENVELOPE>";
string url = "https://dev1.tallyenterprise.com";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
byte[] requestInFormOfBytes = System.Text.Encoding.ASCII.GetBytes(xmlMessage);
request.Method ="POST";
request.ContentType = "text/xml;charset=utf-8";
request.ContentLength = requestInFormOfBytes.Length;
Stream requestStream = request.GetRequestStream();
requestStream.Write(requestInFormOfBytes, 0, requestInFormOfBytes.Length);
requestStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader respStream = new StreamReader(response.GetResponseStream(), System.Text.Encoding.Default);
string receivedResponse = respStream.ReadToEnd();
Label1.Text = receivedResponse;
response.Close();
respStream.Close();
}
如果有人就此工作,请建议我......
答案 0 :(得分:0)
试试这个
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();
}