.net中如何采用POST方式调用WebService?

Luckeryin 2010-02-24 11:15:17
.Net项目中需要调用一个只允许POST方式的WebService.

我在"添加Web引用"的窗口的地址中输入WebService的地址,返回404(未找到)的错误信息.服务器上的日志显示采用了GET方式.我不知道怎么改用POST方式来请用服务.

另外,我从网上下载了一个动态调用WebService的类,并调用其中的QuerySoapWebService方法,其中明确指定的POST方式,但还是返回同样的错误信息.
private static XmlDocument QuerySoapWebService(String url, String methodName, Hashtable Pars, string XmlNs,string cerPath)
{
_xmlNamespaces[url] = XmlNs;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";
request.Headers.Add("SOAPAction", "\"" + XmlNs + (XmlNs.EndsWith("/") ? "" : "/") + methodName + "\"");
SetWebRequest(request, cerPath);
byte[] data = EncodeParsToSoap(Pars, XmlNs, methodName);
WriteRequestData(request, data);
XmlDocument doc = new XmlDocument(), doc2 = new XmlDocument();
doc = ReadXmlResponse(request.GetResponse()); //这里返回错误信息了.
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("soap", "http://schemas.xmlsoap.org/soap/envelope/");
String RetXml = doc.SelectSingleNode("//soap:Body/*/*", mgr).InnerXml;
doc2.LoadXml("<root>" + RetXml + "</root>");
AddDelaration(doc2);
return doc2;
}
请帮我分析分析,谢谢啦.
...全文
396 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
Luckeryin 2010-03-15
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ywjq 的回复:]
XMLDoc = new System.Xml.XmlDocument();
try
{
// post document
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();……
[/Quote]

感谢你的回复,问题已解决,方法和你的很相似。结贴给分。
哈哈 2010-03-10
  • 打赏
  • 举报
回复
XMLDoc = new System.Xml.XmlDocument();
try
{
// post document
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
Byte[] byte1 = encoding.GetBytes(xmlRequestData.OuterXml);
WebRequest HttpWReq = WebRequest.Create(InterfaceURL);
HttpWReq.ContentType = "text/xml";
HttpWReq.ContentLength = xmlRequestData.OuterXml.Length;
HttpWReq.Method = "POST";
// get response
System.IO.Stream StreamData = HttpWReq.GetRequestStream();
StreamData.Write(byte1, 0, byte1.Length);
WebResponse HttpWRes = HttpWReq.GetResponse();
System.IO.Stream receiveStream = HttpWRes.GetResponseStream();
XMLDoc.Load(receiveStream);
StreamData.Close();
}
catch (WebException e3){}


项目中的一段POST方式调用WEBSERVICE的代码,供参考。
Luckeryin 2010-02-25
  • 打赏
  • 举报
回复
第二天了,还是无人问津,自己顶起.
Luckeryin 2010-02-24
  • 打赏
  • 举报
回复
怎么没有人回我呀?继续在线等待中...

12,162

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 Web Services
社区管理员
  • Web Services社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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