• C#调用webservice接口


    1.调用已有webservice,实现天气查询

    2.创建一个简单的webservice,实现服务接口调用

      因为两个实例在一个项目中呈现的,就综合一点来记录,为以后自己需要用时做准备。

      首先,来看下项目的目录结构

     对于初学者来说,在vs2012环境中,大家有可能不知道Web References文件夹是如何来的,它是通过右击项目,选择其中的”添加服务引用“,如图所示:

     之后,得到”添加服务引用“的页面,我也把图截了下来,如图:

     再选择”高级“按钮,进入下图

     根据上图的指示,在URL这栏输入互联网上公开的WebServices(http://www.webxml.com.cn/WebServices/WeatherWebService.asmx)来实现天气预报。这样就可以通过代码来获取用户输入城市的天气了。实现的效果图如下:

     通过点击按钮,系统会输出用户所填城市的天气信息。后台代码如下:

    protected void Button2_Click(object sender, EventArgs e)
    {
                WebApplication1.cn.com.webxml.www.WeatherWebService ws = new WebApplication1.cn.com.webxml.www.WeatherWebService();
                string[] r = ws.getWeatherbyCityName(this.TextBox4.Text);
                this.TextBox3.Text = "";
                if (r == null)
                {
                    this.TextBox3.Text = "无" + this.TextBox4.Text + "城市的天气信息";
                    return;
                }
                foreach (string i in r)
                {
                    this.TextBox3.Text += i;
                }
    }
    

    这样,通过调用已有的天气接口,就可以实现城市天气的查询了。

      下面,我们再来说说,如何自己创建webservice,通过它来实现一些功能,首先,我们选中一个项目,右击新建一个“Web服务”,如下图所示:

     完成创建以后,就会在项目文件中出现“Service.asmx”文件,我们在Service.asmx.cx中添加一个简单的求和方法,代码如下:

    namespace Web_Service
    {
        /// <summary>
        /// WebService1 的摘要说明
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消注释以下行。 
        // [System.Web.Script.Services.ScriptService]
        public class WebService1 : System.Web.Services.WebService
        {
    
            [WebMethod]
            public string HelloWorld()
            {
                return "Hello World";
            }
    
            [WebMethod]
            public int GetSum(int a,int b)
            {
                return a+b;
            }
    
        }
    }
    

      

     那么,我们怎么在其它项目中调用这个方法呢,其实这和刚才我们所说的,调用天气的webservice是一个道理,首先,通过添加“web服务引用”将,你写的webservice引用进来,我们需要注意的是其中有一处要我们填写请求webservice的URL地址,我们该怎么写?其实呢,也很简单,就是,你将Service.asms在浏览器中浏览的地址,如我的是:http://localhost:12197/Service.asmx。

      引用完成后,我们通过代码来实现它,首先还是来看下运行后的效果图:

     最后,再贴上代码:

    protected void Button1_Click(object sender, EventArgs e)
    {
                WebApplication1.localhost.WebService1 aa = new WebApplication1.localhost.WebService1();
                this.Label1.Text = aa.GetSum(Convert.ToInt32(this.TextBox1.Text), Convert.ToInt32(this.TextBox2.Text)).ToString();
    }
    

      

  • 相关阅读:
    UILabel 自适应大小
    CGAffineTransformScale
    CAKeyframeAnimation
    CABasicAnimation
    IOS 动画
    xcode 怎么样在发布release版本的时候 不输出log
    [转贴] C++ 判断主机是否处于联网状态下
    [转贴] 从零开始学C++之异常(二):程序错误、异常(语法、抛出、捕获、传播)、栈展开
    [转贴]从零开始学C++之异常(一):C语言错误处理方法、C++异常处理方法(throw, try, catch)简介
    [转贴]gsoap使用心得!
  • 原文地址:https://www.cnblogs.com/hushzhang/p/16443372.html
Copyright © 2020-2023  润新知