• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#调用Webservice的代码实现方式汇总

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1-添加服务引用;

  (1)工程 - Service References - 添加服务引用;

    

 

   (2)在“添加服务引用”中输入Webservice网站发布地址,并敲回车或点击“转到”;

    

 

   (3)调用代码编写

     实现方式:ServiceReference1.Service1SoapClient ser1 = new ServiceReference1.Service1SoapClient("Service1SoapClient");

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WebServiceTestApp.ServiceReference1;//添加引用

namespace WebServiceTestApp
{
    public partial class Form1 : Form
    {
        ServiceReference1.Service1SoapClient myWebService = null;//设置变量
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            myWebService = new ServiceReference1.Service1SoapClient("Service1Soap");//实例化变量
        }

        private void btnUpLoadCycleTime_Click(object sender, EventArgs e)
        {
            //test
            string SerialNumber = "160719070002873";
            string StationName = "3120";
            string MachineName = @"http://xx.xx.xx.xxx:8001/Service.asmx?";
            string Employee = "user";
            string Password = "123";
            float CycleTime = 0;
            bool Result = false;
            DataTable ChildSNList = null;
            string Message = String.Empty;
            //调用Webservice方法如下
            ServiceReference1.Service1SoapClient ser1 = new ServiceReference1.Service1SoapClient("Service1SoapClient");
            myWebService.UpLoadCycleTime(SerialNumber, StationName, MachineName,
                Employee, Password, CycleTime,
                out Message);
        }
}
}

  

2-添加Web引用

  (1)工程添加Webservice方法如下  

    

 

     

 

     

 

     

 

   (2)C#调用Webservice代码实现  

private void btnStationManage_Click(object sender, EventArgs e)
        {
            //test
            WebReference.Service1 webservice = new WebReference.Service1();
            
            //测试接口过站Json如下:
            string strJson = "[{\"serialnumber\":\"999919250000001\"}]";
            DataTable Output_table = null;
            string result = String.Empty;
            Output_table = webservice.Station_Manage(strJson, out result);

            //测试前进站检查Json如下:
            string strJson2 = "[{\"serialnumber\":\"999919250000001\"}]";
            DataTable Output_table2 = null;
            string result2 = String.Empty;
            Output_table2 = webservice.Station_Manage(strJson2, out result2);

        }

3-System.Net.WebRequest

  实现方式:System.Net.WebRequest webObj = System.Net.WebRequest.Create("http://xx.xx.xx.xxx:xxxx/InterfaceService.asmx/DownLoadSO?sono=" + tbox_so.Text.Trim() + "");

protected void btn_SoSubmit_Click(object sender, EventArgs e)
        {
            string strResult = "";
            try
            {
                if (string.IsNullOrEmpty(tbox_so.Text.Trim()))
                {
                    strResult = "{\"Result\":\"FAIL\",\"Data\":\"FAIL,请输入销售单!\"}";
                    return;
                }
                System.Net.WebRequest webObj = System.Net.WebRequest.Create("http://xx.xx.xx.xxx:xxxx/InterfaceService.asmx/DownLoadSO?sono=" 
          + tbox_so.Text.Trim() + ""); StreamReader streamOjb = new StreamReader(webObj.GetResponse().GetResponseStream(), System.Text.Encoding.Default); strResult = streamOjb.ReadLine(); streamOjb.Close(); } catch (Exception ex) { strResult = "{\"Result\":\"FAIL\",\"Data\":\"FAIL," + ex.Message + "\"}"; } finally { if (strResult.Contains("{\"Result\":\"FAIL\"")) { lbl_msg_so.ForeColor = System.Drawing.Color.Red; } else { lbl_msg_so.ForeColor = System.Drawing.Color.Green; } strResult = strResult.Substring(strResult.IndexOf("Data") + 7); lbl_msg_so.Font.Size = FontUnit.Larger; lbl_msg_so.Text = strResult.Substring(0, strResult.Length - 2); } }

 

4-HTTP POST 调用WebMethod

var url = "http://localhost:51013/Service1.asmx/GetData"; 
var values = new System.Collections.Specialized.NameValueCollection(); values.Add("data", "hi server"); 
var client = new WebClient(); 
var data = client.UploadValues(url, "POST", values); 
var result = System.Text.Encoding.UTF8.GetString(data); 
Console.WriteLine(result); 
// 输出: // <?xml version="1.0" encoding="utf-8"?> // <string xmlns="http://tempuri.org/">You enter:hi server</string>

 5-HTTP GET调用WebMethod

var url3 = "http://localhost:51013/Service1.asmx/GetData3?data='test'"; 
var client3 = new WebClient(); 
client3.Headers.Add("Content-Type", "application/json"); 
var result3 = client3.DownloadString(url3); 
Console.WriteLine(result3); 
//输出: //You enter:test

注意:GET调用时是通过Url传值,如果是string需要使用 data='test' 两边加上单引号,因为使用json反序列化。没有引号直接当成是数值转换会出错。(将抛出 500 的服务端错误)


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap