C#方式Post方式发送xml及接收
RT,我需要做个demo aa.aspx.cs里面post一个xml格式数据到bb.aspx页面该如何做?我试了以下代码
[align=left] string postXml = "<?xml version=\"1.0\" encoding=\"GBK\"?><sync_req><head><id>WXCS</id><pwd>zs@0822*hxyd</pwd></head>" +
"<body><area>0760</area><serviceid>ZSWX</serviceid><appName>SZtest</appName><phoneList><phone><phoneNum></phoneNum>" +
"<action>A</action> <position_type>A</position_type><ue_type>1</ue_type><servicetype>AL</servicetype><status>S</status> <locationTime>1</locationTime>" +
"</phone> </phoneList></body></sync_req>";
string url = "http://localhost:3496/BusRoute.aspx";
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = postXml.Length;
Stream newStream = myRequest.GetRequestStream();
byte[] data = Encoding.GetEncoding("GB2312").GetBytes(postXml);
// 发送数据
newStream.Write(data, 0, data.Length);
HttpWebResponse res = myRequest.GetResponse() as HttpWebResponse;
StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8);
String ret = sr.ReadToEnd();
XmlDocument doc = new XmlDocument();
doc.LoadXml(ret);
string mes = doc.GetElementsByTagName("resultInfo")[0].InnerText;[/align]
结果在HttpWebResponse res = myRequest.GetResponse() as HttpWebResponse;执行到这句是报
远程服务器返回错误: (500) 内部服务器错误。
代码没有问题,可是url改成“http://120.197.89.32/ELIS/lbsServle”就可以正确返回结果,现在我想在本地程序http://localhost:3496/BusRoute.aspx测试改怎么做?
求高手解决!!!!! --------------------编程问答-------------------- XmlDocument XmlD=new XmlDocument();
Xml.LoadXml(Path);
RequestXml.OuterXml
Stream oWriter=null;
bool bResult=true;
byte[] data = System.Text.Encoding.UTF8.GetBytes(Xml.OuterXml); //转换xml内的数据为byte的数组
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); //url,函数接口地址
myRequest.Method = "POST ";
myRequest.Headers.Add( "SOAPAction ", SoapAction); //指定访问Web Service的命名空间及使用方法
myRequest.Headers.Add( "ContentType ", "text/xml; charset=utf-8 "); //指定字符集
myRequest.Headers.Add( "ContentLength ",data.Length.ToString());
try
{
oWriter=myRequest.GetRequestStream();//获取与远程Web Service的联络
oWriter.Write(data,0,data.Length);//发送数据
oWriter.Close(); //关闭连接
}
catch(Exception)
{
//oWriter.Close();
bResult=false;
}
Stream oReader=null;
if (bResult) //只有发送数据正常时,才读取响应
{
try
{
HttpWebResponse oResponse=(HttpWebResponse)myRequest.GetResponse();
oReader =oResponse.GetResponseStream() ;
outxml.Load(oReader);
oReader.Close();
}
catch(Exception )
{
}
}
补充:.NET技术 , ASP.NET