C#调用Webservice的几种方式

2021-05-17 17:31

阅读:434

标签:void   output   protect   ons   回车   mamicode   download   partial   system   

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-待续

C#调用Webservice的几种方式

标签:void   output   protect   ons   回车   mamicode   download   partial   system   

原文地址:https://www.cnblogs.com/turnip/p/11764126.html


评论


亲,登录后才可以留言!