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

怎么使用webservice

我的项目需要生成一个查询公告列表,但数据来源是另一个项目,那边做了一个webService给我

不会用啊,求教怎么使用(可以说下详细的过程吗)。。。他提供了接口、xmlParam参数、返回值说明[xml格式]

webservice --------------------编程问答-------------------- 项目-添加web引用-输入web服务的地址,确定。

然后VS会为你创建一个web service的包装类,你创建一个,就像调用一般对象的方法即可。 --------------------编程问答-------------------- 看看介个 --------------------编程问答-------------------- 建议手头有本关于webservice或者数扭异构等方面的书 --------------------编程问答-------------------- 手动创建HttpWebRequest请求:

示例:
天气预报WebService,链接地址为:
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getSupportCity
打开该网页后,有以下描述:
POST /WebServices/WeatherWebService.asmx HTTP/1.1
Host: www.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getSupportCity"

<?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>
    <getSupportCity xmlns="http://WebXml.com.cn/">
      <byProvinceName>string</byProvinceName>
    </getSupportCity>
  </soap:Body>
</soap:Envelope>

然后根据描述手动创建HttpWebRequest请求,设置各项参数即可

代码如下:
private void WebServiceTest()
{
// 请求的XML格式字符串
string xmlParser = "<?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><getSupportCity xmlns=\"http://WebXml.com.cn/\"><byProvinceName>广东</byProvinceName></getSupportCity></soap:Body></soap:Envelope>";
byte[] b = System.Text.Encoding.UTF8.GetBytes(xmlParser);

// WebService地址(从POST /WebServices/WeatherWebService.asmx HTTP/1.1 和 Host: www.webxml.com.cn可获取)
string url = @"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ProtocolVersion = new Version(1, 1); // HTTP协议版本(POST /WebServices/WeatherWebService.asmx HTTP/1.1 可获取)
request.Headers.Add("SOAPAction", @"http://WebXml.com.cn/getSupportCity"); // (从SOAPAction: "http://WebXml.com.cn/getSupportCity" 可获取)
request.ContentType = @"text/xml; charset=utf-8";
request.Accept = @"text/xml";
request.ContentLength = b.Length;
request.Method = @"Post";

Stream stream = request.GetRequestStream();
stream.Write(b, 0, b.Length);
stream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// do sonething
}
--------------------编程问答-------------------- 添加web引用 就跟正常方法一样了 --------------------编程问答--------------------
引用 1 楼 caozhy 的回复:
项目-添加web引用-输入web服务的地址,确定。

然后VS会为你创建一个web service的包装类,你创建一个,就像调用一般对象的方法即可。


在吗,请问从WebService获取到了XML文件,在吗解析出来绑定到Repeater中,要绑定显示ktsj、ay
xml文件格式是:
<?xml version="1.0" encoding="GBK"?>
      <KtggList total="1085">
        <Ktgg bh="" ah="(2011)湘高法刑三终字第00178号" ktsj="2011-11-22 09:00:00.0" ktdd="" cbr="" cbts="" ay="" fgrq="2011-12-22">
           <Dsr mc=""/>
           <Sjy mc=""/>
        </Ktgg>
       </KtggList> --------------------编程问答--------------------
引用 4 楼 szc5566 的回复:
手动创建HttpWebRequest请求:

示例:
天气预报WebService,链接地址为:
http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?op=getSupportCity
打开该网页后,有以下描述:
POST /WebServices/WeatherWebService.asmx HTTP/1.1
Host: www.webxml.com.cn
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://WebXml.com.cn/getSupportCity"

<?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>
    <getSupportCity xmlns="http://WebXml.com.cn/">
      <byProvinceName>string</byProvinceName>
    </getSupportCity>
  </soap:Body>
</soap:Envelope>

然后根据描述手动创建HttpWebRequest请求,设置各项参数即可

代码如下:
private void WebServiceTest()
{
// 请求的XML格式字符串
string xmlParser = "<?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><getSupportCity xmlns=\"http://WebXml.com.cn/\"><byProvinceName>广东</byProvinceName></getSupportCity></soap:Body></soap:Envelope>";
byte[] b = System.Text.Encoding.UTF8.GetBytes(xmlParser);

// WebService地址(从POST /WebServices/WeatherWebService.asmx HTTP/1.1 和 Host: www.webxml.com.cn可获取)
string url = @"http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ProtocolVersion = new Version(1, 1); // HTTP协议版本(POST /WebServices/WeatherWebService.asmx HTTP/1.1 可获取)
request.Headers.Add("SOAPAction", @"http://WebXml.com.cn/getSupportCity"); // (从SOAPAction: "http://WebXml.com.cn/getSupportCity" 可获取)
request.ContentType = @"text/xml; charset=utf-8";
request.Accept = @"text/xml";
request.ContentLength = b.Length;
request.Method = @"Post";

Stream stream = request.GetRequestStream();
stream.Write(b, 0, b.Length);
stream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// do sonething
}



在吗,请问从WebService获取到了XML文件,在吗解析出来绑定到Repeater中,要绑定显示ktsj、ay
 xml文件格式是:
<?xml version="1.0" encoding="GBK"?>
       <KtggList total="1085">
         <Ktgg bh="" ah="(2011)湘高法刑三终字第00178号" ktsj="2011-11-22 09:00:00.0" ktdd="" cbr="" cbts="" ay="" fgrq="2011-12-22">
            <Dsr mc=""/>
            <Sjy mc=""/>
         </Ktgg>
        </KtggList>  --------------------编程问答--------------------  那你直接Google一下webservice的使用实例吧
补充:.NET技术 ,  Web Services
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,