C# 访问WebService

 

using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace PAMS2CE
{
    public partial class UpdateForm : PAMS2CE.UI.BaseForm
    {
        private WebReference.route[] routeList = null;

        private string StrServiceIP = "";
        private enum DownUp_Progress : int
        {
            StepOne = 20,
            StepTwo = 40,
            StepThree = 60,
            StepFour = 80,
            StepFive = 100
        }


        NetworkManager networkMgr = new NetworkManager();
        public NetworkManager NetworkMgr
        {
            get { return networkMgr; }
            set { networkMgr = value; }
        }

        public UpdateForm()
        {
            InitializeComponent();
            networkMgr.ConnectNotify += new ConnectNotifyEventHandler(ConnectNotify);
            Application.DoEvents();
        }


        private void MainForm_Load(object sender, EventArgs e)
        {
            datepicStart.CustomFormat = "yy-MM-dd";
            datepicEnd.CustomFormat = "yy-MM-dd";

            panelDataUpload.Enabled = false;
            panelRouteDownload.Enabled = false;
            rbDataUpload.Enabled = false;
            rbRouteDownload.Enabled = false;


            string str = Const.GetPamsInfor(Const.PAMSInfo.PAMS_USBWIFT);
            if (str == "1")//wifi
            {
                StrServiceIP = Const.GetPamsInfor(Const.PAMSInfo.PAMS_WSIP);
                MyCEDLL.WiFi_Power_On();
            }

            networkMgr.Connect(ConnType.Wlan);
            networkMgr.Start();
        }

        #region 网络状态检查
        /// <summary>
        /// 检查网络连接和传输数据
        /// </summary>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        public void ConnectNotify(object sender, ConnectNotifyEventArgs e)
        {
            if (this.InvokeRequired)
            {
                object[] param = new object[2] { sender, e };
                this.BeginInvoke(new ConnectNotifyEventHandler(OnConnectNotify), param);
            }
            else
            {
                OnConnectNotify(sender, e);
            }
        }

        /// <summary>
        /// 检查网络连接和传输数据
        /// </summary>
        /// <param name="wParam"></param>
        /// <param name="lParam"></param>
        private void OnConnectNotify(object sender, ConnectNotifyEventArgs e)
        {
            try
            {
                #region Signal
                switch (e.ConnectResult)
                {
                    case ConnState.ConnectSuccess:
                        {
                            switch (e.ConnectType)
                            {
                                case ConnType.Gprs:
                                    this.lblNetState.Text = "G已连接";
                                    break;
                                case ConnType.Wlan:
                                    //int sigStrength = Device.GetWlanSignalStrength();
                                    this.lblNetState.Text = "已连接";
                                    rbDataUpload.Enabled = true;
                                    rbRouteDownload.Enabled = true;
                                    if (!rbDataUpload.Checked)//如果选择了数据上传,则路线下载的panel不使能
                                        panelRouteDownload.Enabled = true;
                                    break;
                                default:
                                    this.lblNetState.Text = "未连接";
                                    break;
                            }
                        }
                        break;
                    case ConnState.Connecting:
                        this.lblNetState.Text = "连接...";
                        break;
                    default:
                        this.lblNetState.Text = "未连接";
                        break;
                }
                #endregion
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        #endregion


        #region 单选按钮变化
        private void rbRouteDownload_CheckedChanged(object sender, EventArgs e)
        {
            panelRouteDownload.Enabled = true;
            panelDataUpload.Enabled = false;
            progressBar.Value = 0;

        }

        private void rbDataUpload_CheckedChanged(object sender, EventArgs e)
        {
            panelRouteDownload.Enabled = false;
            panelDataUpload.Enabled = true;
            progressBar.Value = 0;
        }

        #endregion

 

        private void btnDownload_Click(object sender, EventArgs e)
        {
            int totalCount = lvRoute.Items.Count;
            int selectCount  = 0;

            for (int i = 0; i < totalCount; i++)
            {
                if (lvRoute.Items[i].Checked)
                {
                    selectCount++;
                }
            }

            if (selectCount <= 0)
            {
                MessageBox.Show("未选择!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                return;
            }

            int[] rids = new int[selectCount];
            WebReference.route[] routeSelList = new PAMS2CE.WebReference.route[selectCount];
            int index = 0;
            for (int i = 0; i < selectCount; i++)
            {
                if (lvRoute.Items[i].Checked)
                {
                    rids[index] = Int32.Parse(lvRoute.Items[i].SubItems[1].Text);
                    routeSelList[index] = routeList[i];
                    index++;
                }
            }

            WebReference.TIPMService tipm = new PAMS2CE.WebReference.TIPMService();
            if (tipm == null)
                return;
            try
            {
                string newURL = GetURL();
                if (newURL != null && tipm.Url != newURL)
                    tipm.Url = newURL;

                progressBar.Value = 0;
                UpDownModel.ClearUpdateCFGDB();
                progressBar.Value = 10;

                WebReference.cycleInfo[] cycleInfoList = tipm.GetCycleInfoList(rids);
                if (cycleInfoList == null)
                {
                    MessageBox.Show("请重新下载!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                    progressBar.Value = 0;
                    return;
                }
                else
                {
                    if (UpDownModel.insertCycle(cycleInfoList))
                    {
                        progressBar.Value = 20;
                    }
                    else
                    {
                        MessageBox.Show("失败,请重新下载!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                        progressBar.Value = 0;
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
            }
            if (tipm != null)
            {
                tipm.Abort();
            }
        }

 

        private void btnUpload_Click(object sender, EventArgs e)
        {
            progressBar.Value = 0;
            WebReference.TIPMService tipm = new PAMS2CE.WebReference.TIPMService();
            try
            {
                if (tipm == null)
                    return;

                string newURL = GetURL();
                if (newURL != null && tipm.Url != newURL)
                    tipm.Url = newURL;


                WebReference.djResult[] djresultList = UpDownModel.GetDJResultByStartEndTime(datepicStart.Value, datepicEnd.Value);
                if (djresultList != null)
                {
                    progressBar.Value = 20;
                    WebReference.uploadInfo upInfo = tipm.UploadDjResult(djresultList);
                    //if (!upInfo.BSuccess)
                    //{
                    //    MessageBox.Show(upInfo.errorINFO);
                    //    return;
                    //}
                    progressBar.Value = 50;
                }
                else
                {
                    MessageBox.Show("失败!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                    return;
                }

                WebReference.sampleDats[] sampleList = UpDownModel.GetSampleDataByStartEndTime(datepicStart.Value, datepicEnd.Value);
                if (sampleList != null)
                {
                    progressBar.Value = 70;
                    WebReference.uploadInfo upInfo = tipm.UploadVibData(sampleList);
                    if (!upInfo.BSuccess)
                    {
                        MessageBox.Show(upInfo.errorINFO);
                        return;
                    }
                }

                if (DialogResult.OK == MessageBox.Show("提示", "信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1))
                {
                    if (false == DBConn.ClearDATDB())
                    {
                                               return;
                    }
                }
            }
            catch(Exception ex)
            {
                progressBar.Value = 0;
                MessageBox.Show(ex.Message.ToString());
            }
            if (tipm != null)
            {
                tipm.Abort();
            }
        }


        private string GetURL()
        {
            return "http://" + StrServiceIP + ":Web Service";
        }


        private void btnGetRoute_Click(object sender, EventArgs e)
        {
            if (textboxID.Text == "")
            {
                MessageBox.Show("失败", "信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                return;
            }
            WebReference.TIPMService tipm = new PAMS2CE.WebReference.TIPMService();

            try
            {
                if(tipm == null)
                {
                    return;
                }

                string newURL = GetURL();
                if (newURL != null && tipm.Url != newURL)
                    tipm.Url = newURL;

                progressBar.Value = (int)DownUp_Progress.StepOne;
                WebReference.routeUser user = tipm.GetUserRegInfo(textboxID.Text, textboxPWD.Text);
                if (user == null)
                {
                    return;
                }
                progressBar.Value = (int)DownUp_Progress.StepTwo;
                routeList = tipm.GetRouteListByUser(user.ID);
                if (routeList == null)
                {
                    return;
                }
                progressBar.Value = (int)DownUp_Progress.StepThree;
                int length = routeList.Length;
                lvRoute.Items.Clear();
                for (int i = 0; i < length; i++)
                {
                    ListViewItem lvi = new ListViewItem();
                    lvi.Text = routeList[i].routeName;

                    lvi.SubItems.Add(routeList[i].RID.ToString());
                    lvRoute.Items.Add(lvi);
                }
                progressBar.Value = (int)DownUp_Progress.StepFive;
            }
            catch (Exception ex)
            {
                progressBar.Value = 0;
                MessageBox.Show(ex.Message.ToString(), "信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
                Log.E("UpdateForm",ex.Message.ToString());
            }
            if (tipm != null)
            {
                tipm.Abort();
            }
        }


        private void btnBack_Click(object sender, EventArgs e)
        {
            MyCEDLL.WiFi_Power_Off();
            networkMgr.Stop();
            this.Close();
        }

 

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值