⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netaccess.cs

📁 微软的行业应用解决方案实例!非常优秀的Windows Mobile开发案例
💻 CS
字号:
using System;
using System.Net;
using System.IO;
using System.Drawing;
using HardwareDistributor.net.mappoint.staging;


namespace HardwareDistributor.Data
{
    /// <summary>
    /// Data layer class that interacts with the network
    /// </summary>
    public class NetAccess
    {
 
        /// <summary>
        /// WebCheck sends a GET request to a valid URL
        /// like a web page, web service, or replication ISAPI dll
        /// </summary>
        /// <param name="url"></param>
        /// <param name="cred"></param>
        /// <returns></returns>
        public static bool webCheck(string url, NetworkCredential cred)
        {
            HttpWebRequest _req = null;
            HttpWebResponse _res = null;
            Uri _uri = null;

            try
            {
                //Create a Uri object
                _uri = new Uri(url);
                //Create an HttpWebRequest object
                _req = (HttpWebRequest)WebRequest.Create(_uri);
                //Set the HttpWebRequest's properties
                _req.Timeout = 20000;
                _req.KeepAlive = false;
                _req.Credentials = cred;

                //Return an HttpWebResponse object
                _res = (HttpWebResponse)_req.GetResponse();
                //Check for null
                if (_res != null)
                {
                    //If Uri was reached sucessfully return true
                    if (_res.StatusCode == HttpStatusCode.OK)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                return false;
            }
            finally
            {
                //Ensure HttpWebResponse is closed
                if (_res != null)
                {
                    _res.Close();
                    _res = null;
                }
                //Ensure HttpWebRequest is closed
                if (_req != null)
                {
                    _req.Abort();
                    _req = null;
                }
            }
        }
        
        
        /// <summary>
        /// Instantiate MapPoint web services and call them as appropriate
        /// </summary>
        /// <param name="height"></param>
        /// <param name="width"></param>
        /// <param name="address"></param>
        /// <param name="city"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public static Bitmap CallMapPoint(int height, int width, string address, string city, string state)
        {
            //Configure Credentials
            NetworkCredential _credentials = new NetworkCredential(Business.GlobalCache.Instance.MapPointUserName, Business.GlobalCache.Instance.MapPointPassword);

            //Configure FindServiceSoap
            FindServiceSoap _find = new FindServiceSoap();
            _find.Credentials = _credentials;
            _find.PreAuthenticate = true;
            _find.Url = Business.GlobalCache.Instance.MapPointFindServiceUrl;

            //Configure RenderServiceSoap
            RenderServiceSoap _render = new RenderServiceSoap();
            _render.Credentials = _credentials;
            _render.PreAuthenticate = true;
            _render.Url = Business.GlobalCache.Instance.MapPointRenderServiceUrl;

            //Configure RouteServiceSoap
            RouteServiceSoap _route = new RouteServiceSoap();
            _route.Credentials = _credentials;
            _route.PreAuthenticate = true;
            _route.Url = Business.GlobalCache.Instance.MapPointRouteServiceUrl;

            //Get the Lat and Long for the distribution center starting point
            Location _start = GetLatLong("1 Microsoft Way", "Redmond", "WA", _find);
            //Get the Lat and Long for the customer end point
            Location _end = GetLatLong(address, city, state, _find);
            //Get the route from the distribution center to the customer
            return GetRoute(_start, _end, _route, _render, height, width);

        }


        /// <summary>
        /// Get the Lat and Long based on an address
        /// </summary>
        /// <param name="address"></param>
        /// <param name="city"></param>
        /// <param name="state"></param>
        /// <param name="find"></param>
        /// <returns></returns>
        private static Location GetLatLong(string address, string city, string state, FindServiceSoap find)
        {
            //Create an address object
            Address _address = new Address();
            //Set the address object's properties
            _address.AddressLine = address;
            _address.PrimaryCity = city;
            _address.Subdivision = state;

            //Create a FindAddressSpecification object
            FindAddressSpecification _findAddressSpec = new FindAddressSpecification();
            //Set the FindAddressSpecification's properties
            _findAddressSpec.DataSourceName = "MapPoint.NA";
            _findAddressSpec.InputAddress = _address;

            //Retrieve address information
            FindResults _results = find.FindAddress(_findAddressSpec);

            //If address information exists
            if (_results.NumberFound == 0)
            {
                return null;
            }
            else
            {
                //Return the Location
                return _results.Results[0].FoundLocation;
            }
        }


        /// <summary>
        /// Get the route map that the driver needs to follow
        /// </summary>
        /// <param name="Start"></param>
        /// <param name="End"></param>
        /// <param name="route"></param>
        /// <param name="render"></param>
        /// <param name="height"></param>
        /// <param name="width"></param>
        /// <returns></returns>
        private static Bitmap GetRoute(Location Start, Location End, RouteServiceSoap route, RenderServiceSoap render, int height, int width)
        {
            //Creat a LatLong array 
            LatLong[] _latlongs = new LatLong[2];
            //Add the starting location
            _latlongs[0] = new LatLong();
            _latlongs[0] = Start.LatLong;
            //Add the ending location
            _latlongs[1] = new LatLong();
            _latlongs[1] = End.LatLong;

            //Create a MapOptions object
            MapOptions _options = new MapOptions();
            //Set the MapOptions' properties
            _options.Format = new ImageFormat();
            _options.Format.Height = height;
            _options.Format.Width = width;

            //Return a North American route based on the start and end lat and long
            Route _route = route.CalculateSimpleRoute(_latlongs, "MapPoint.NA", SegmentPreference.Quickest);
                        
            //Create a MapSpecification object
            MapSpecification _mapSpec = new MapSpecification();
            //Set the MapSpecification's properties
            _mapSpec.DataSourceName = "MapPoint.NA";
            _mapSpec.Options = _options;
            _mapSpec.Route = _route;
            
            //Call the MapPoint web service to return an array of maps
            MapImage[] _images = render.GetMap(_mapSpec);
            
            //Return the first map as a Bitmap object
            return new Bitmap(new MemoryStream(_images[0].MimeData.Bits));
        }

    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -