当前位置:编程学习 > C#/ASP.NET >>

HttpWebRequest 调用 WSDL

这个是我用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有两个以上的方法,我怎么做.
谢谢! --------------------编程问答-------------------- 先自己顶一下. --------------------编程问答-------------------- this.ServiceUrl+"/方法名" --------------------编程问答--------------------
引用 2 楼 feiyun0112 的回复:
this.ServiceUrl+"/方法名"
this.ServiceUrl 是个WSDL的地址:类似这样 "https://...../Services?WSDL"
然后,我试了this.ServiceUrl+"/方法名",这养还是报错:The remote server returned an error: (500) Internal Server Error. --------------------编程问答-------------------- 要分开来写 --------------------编程问答-------------------- 之前我做的一个项目用到的,希望对你有帮助
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)
--------------------编程问答-------------------- 上面的
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/'>");
补充:.NET技术 ,  Web Services
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,