C#方式Post方式发送xml及接收

韦德 2011-11-23 10:05:23
RT,我需要做个demo aa.aspx.cs里面post一个xml格式数据到bb.aspx页面该如何做?
我试了以下代码


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;




结果在HttpWebResponse res = myRequest.GetResponse() as HttpWebResponse;执行到这句是报
远程服务器返回错误: (500) 内部服务器错误。

代码没有问题,可是url改成“http://120.197.89.32/ELIS/lbsServle”就可以正确返回结果,现在我想在本地程序http://localhost:3496/BusRoute.aspx测试改怎么做?

求高手解决!!!!!
...全文
604 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
csdn_aspnet 2011-11-23
  • 打赏
  • 举报
回复
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 )
{
}
}

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧