HttpWebRequest 调用 WSDL

lrl1286219215 2013-12-13 04:58:48
这个是我用HttpWebRequest 调用 wsdl 的代码:

httpWebRequest = (HttpWebRequest)WebRequest.Create(this.ServiceUrl);
httpWebRequest.Headers["Accept-Encoding"] = "deflate";
httpWebRequest.Headers["SOAPAction"] = string.Empty;
httpWebRequest.Accept = "text/xml";
httpWebRequest.ContentType = "text/xml";
httpWebRequest.Method = "POST";

var rb = Encoding.UTF8.GetBytes(this.Message);
httpWebRequest.ContentLength = rb.Length;
using (var requestStream = httpWebRequest.GetRequestStream())
{
requestStream.Write(rb, 0, rb.Length);
}

using (var response = httpWebRequest.GetResponse())
using (var responseStream = response.GetResponseStream())
{
if (responseStream == null)
{
return new SOAPResponse
{
Status = ResponseStatus.Success,
};
}

using (var reader = new StreamReader(responseStream))
{
return new SOAPResponse
{
Status = ResponseStatus.Success,
Message = reader.ReadToEnd()
};
}
}

其中this.ServiceUrl 是我 wsdl 的地址,然后,如果说,这个wsdl只有一个方法的时候,上面的方式调用WSDL是可以的,如果WSDL有两个以上的方法,那么上面的代码就会报这个错误:"The remote server returned an error: (500) Internal Server Error."
所以,
各位大神,如果WSDL有两个以上的方法,我怎么做.
谢谢!
...全文
2491 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
df978 2013-12-17
  • 打赏
  • 举报
回复
上面的 MyParam.AppendLine("<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:bai="http://www.baidu.com/">"); 修改成:
MyParam.AppendLine("<soap:Envelope xmlns:soap='http://www.w3.org/2003/05/soap-envelope' xmlns:bai='http://www.baidu.com/'>");
df978 2013-12-17
  • 打赏
  • 举报
回复
之前我做的一个项目用到的,希望对你有帮助 1、webservice
[WebService(Namespace = "http://www.baidu.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[SoapDocumentService(RoutingStyle=SoapServiceRoutingStyle.RequestElement)]

[WebMethod(Description = "测试用,不做处理直接返回输入的内容")]
 public String ReturnTest(String usr, String pwd)
 {
   return "您输入了:‘"+usr + "’ 和 ‘" + pwd +"’";
 }
2、定义接口和接口参数(如果这里的接口和参数不了解,可以通过soapui工具自动生成)
      StringBuilder MyParam

      MyParam.AppendLine("<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:bai="http://www.baidu.com/">");
      MyParam.AppendLine("<soap:Header/>");
      MyParam.AppendLine("<soap:Body>");
      MyParam.AppendLine("<bai:ReturnTest>");
      MyParam.AppendLine("<bai:usr>A008</bai:usr>");
      MyParam.AppendLine("<bai:pwd>123456</bai:pwd>");
      MyParam.AppendLine("</bai:ReturnTest>");
      MyParam.AppendLine("</soap:Body>");
      MyParam.AppendLine("</soap:Envelope>");
3、HttpWebRequest 调用方法
  public string GetGsReturn(string url, StringBuilder param)//提交到接口并返回参数
  {
   string responseString = string.Empty;
   try
   {
    byte[] bs = Encoding.UTF8.GetBytes(param.ToString()); //具体根据实际的编码来修改

    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
    myRequest.Method = "post";
    
   //---------------这个根据实际情况来填写-----------------------
    myRequest.Headers.Add("SOAPAction", "");
    myRequest.Headers.Add("Accept-Encoding", "gzip,deflate");
    myRequest.ContentType = "text/xml; charset=utf-8";

    myRequest.KeepAlive = true;
    myRequest.ContentLength = bs.Length;
   //-------------------------------------------------------------

    Stream reqStream = myRequest.GetRequestStream();
    reqStream.Write(bs, 0, bs.Length);

    HttpWebResponse myResponse;
    try
    { myResponse = (HttpWebResponse)myRequest.GetResponse(); }
    catch (WebException ex)
    { myResponse = (HttpWebResponse)ex.Response; }
    if (myRequest != null)
    {
     StreamReader sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
     responseString = sr.ReadToEnd();
    }
   }
   catch (Exception ex)
   {
    responseString = ex.Message;
   }
   return responseString;
  }
4、调用
GetGsReturn("http://192.168.1.108/Webservice.asmx",MyParam)
风一样的大叔 2013-12-16
  • 打赏
  • 举报
回复
lrl1286219215 2013-12-13
  • 打赏
  • 举报
回复
引用 2 楼 feiyun0112 的回复:
this.ServiceUrl+"/方法名"
this.ServiceUrl 是个WSDL的地址:类似这样 "https://...../Services?WSDL" 然后,我试了this.ServiceUrl+"/方法名",这养还是报错:The remote server returned an error: (500) Internal Server Error.
feiyun0112 2013-12-13
  • 打赏
  • 举报
回复
this.ServiceUrl+"/方法名"
lrl1286219215 2013-12-13
  • 打赏
  • 举报
回复
先自己顶一下.

12,162

社区成员

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

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