none
webservice 接口 接收XML并解析的问题 RRS feed

  • 问题



  •   1.webservice服务器接口,比如:http://www.sina.com/dmb/Service.asmx?op=IPTV2

      2.请求报文,每隔20S 终端会向webservice服务器端提出一次请求,因为要一直发送请求,报文尽量短。

      <?xml version="1.0" encoding="utf-8"?>

      <request website="http://service.cedock.com">

      <parameter type="GetXML" id="XXXXXXXXX">

      </parameter>

      </request>

    请问这个webservices 怎么接收请求。

    由于第一次接触Webservice,能提供相关例子参考一下吗。
    2011年8月2日 6:26

答案



  • 请问这个webservices 怎么接收请求。

    由于第一次接触Webservice,能提供相关例子参考一下吗。


    如果你要调用Sina的WebService,至少需要知道对方的WSDL,例如天气预报WebService的WSDL如下网址:

    http://www.webservicex.net/globalweather.asmx?WSDL

    如果你是用Vitrul Studio 开发环境,你可以添加WebService引用(Add Service Reference=>Advanced=>Add web service reference),

    URL地址输入上面WebService地址。客户端调用代码如下:

     WS.GlobalWeather weatherService = new WS.GlobalWeather();
     string weatherXml = weatherService.GetWeather("Beijing", "China");

     

    如果你想直接发送SOAP请求“报文”,可以如下操作:

        static void Main(string[] args)
        {
          string requestUri = "http://www.webservicex.net/globalweather.asmx?WSDL";
          string soapAction = "http://www.webserviceX.NET/GetWeather";
          string requestContent = @"<?xml version='1.0' encoding='utf-8'?>
                        <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
                         <soap:Body>
                          <GetWeather xmlns='http://www.webserviceX.NET'>
                            <CityName>Beijing</CityName>
                            <CountryName>China</CountryName>
                          </GetWeather>
                         </soap:Body>
                        </soap:Envelope>";
          string response = GetWSResponse(requestUri, soapAction, requestContent);
        }
    
        public static string GetWSResponse(string requestUri, string soapAction, string requestContent)
        {
          string responseContent = string.Empty;
    
          byte[] content = Encoding.UTF8.GetBytes(requestContent);
    
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
    
          request.Credentials = CredentialCache.DefaultCredentials;
          request.Method = "POST";
          request.ContentType = "text/xml; charset=utf-8";
          request.Headers.Add("SOAPAction", soapAction);
          request.ContentLength = content.Length;
          request.KeepAlive = false;
    
    
          using (Stream reqestStream = request.GetRequestStream())
          {
            reqestStream.Write(content, 0, content.Length);
          }
    
          using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
          {
            //if (response.StatusCode == HttpStatusCode.OK) Console.WriteLine("Found");
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(936)))
            {
              responseContent = reader.ReadToEnd();
            }
          }
    
          return responseContent;
        }
    

     

     

    2011年8月5日 2:09

全部回复

  • 接口是服务提供者进行定义的,你得去查sina的文档。
    【孟子E章】
    2011年8月4日 1:24
    版主

  • 请问这个webservices 怎么接收请求。

    你好:)

    可以尝试在已知的webservice路径后面追加参数(比如:http://www.sina.com/dmb/Service.asmx?op=IPTV2&其它参数=其它内容)

    然后通过一个Service对xml进行解析(使用LINQ-TO-XML),或者直接接受参数即可。

     


    如果你有其它意见或私下交流,请直接发送maledong_work@foxmail.com;或者讨论(Talk)
    If you do not have QQ, please open the page and download it and click the image to talk or leave message for me.
    下载MSDN桌面工具(Vista,Win7)
    下载Technet桌面小工具(Vista,Win7)
    慈善点击,点击此处
    2011年8月4日 3:20


  • 请问这个webservices 怎么接收请求。

    由于第一次接触Webservice,能提供相关例子参考一下吗。


    如果你要调用Sina的WebService,至少需要知道对方的WSDL,例如天气预报WebService的WSDL如下网址:

    http://www.webservicex.net/globalweather.asmx?WSDL

    如果你是用Vitrul Studio 开发环境,你可以添加WebService引用(Add Service Reference=>Advanced=>Add web service reference),

    URL地址输入上面WebService地址。客户端调用代码如下:

     WS.GlobalWeather weatherService = new WS.GlobalWeather();
     string weatherXml = weatherService.GetWeather("Beijing", "China");

     

    如果你想直接发送SOAP请求“报文”,可以如下操作:

        static void Main(string[] args)
        {
          string requestUri = "http://www.webservicex.net/globalweather.asmx?WSDL";
          string soapAction = "http://www.webserviceX.NET/GetWeather";
          string requestContent = @"<?xml version='1.0' encoding='utf-8'?>
                        <soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>
                         <soap:Body>
                          <GetWeather xmlns='http://www.webserviceX.NET'>
                            <CityName>Beijing</CityName>
                            <CountryName>China</CountryName>
                          </GetWeather>
                         </soap:Body>
                        </soap:Envelope>";
          string response = GetWSResponse(requestUri, soapAction, requestContent);
        }
    
        public static string GetWSResponse(string requestUri, string soapAction, string requestContent)
        {
          string responseContent = string.Empty;
    
          byte[] content = Encoding.UTF8.GetBytes(requestContent);
    
          HttpWebRequest request = (HttpWebRequest)WebRequest.Create(requestUri);
    
          request.Credentials = CredentialCache.DefaultCredentials;
          request.Method = "POST";
          request.ContentType = "text/xml; charset=utf-8";
          request.Headers.Add("SOAPAction", soapAction);
          request.ContentLength = content.Length;
          request.KeepAlive = false;
    
    
          using (Stream reqestStream = request.GetRequestStream())
          {
            reqestStream.Write(content, 0, content.Length);
          }
    
          using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
          {
            //if (response.StatusCode == HttpStatusCode.OK) Console.WriteLine("Found");
            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(936)))
            {
              responseContent = reader.ReadToEnd();
            }
          }
    
          return responseContent;
        }
    

     

     

    2011年8月5日 2:09