예제 #1
0
        /// <summary>
        /// 开启Windows服务
        /// </summary>
        /// <param name="windowsServiceName">服务名称</param>
        public static MessageInformation StartWindowsService(string windowsServiceName) {
            var messageInformation = new MessageInformation();

            try {
                var isNotExists = ServiceController.GetServices().All(data => data.ServiceName != windowsServiceName);
                if(isNotExists)
                    throw new Exception($"Windows服务{windowsServiceName}不存在");
                using(ServiceController control = new ServiceController(windowsServiceName)) {
                    if(control.Status == ServiceControllerStatus.Stopped) {
                        control.Start();
                        control.WaitForStatus(ServiceControllerStatus.Running);
                        messageInformation.ExecuteSuccess($"{windowsServiceName}调度服务已重新启动");
                    } else if(control.Status == ServiceControllerStatus.Running) {
                        if(!control.CanStop) {
                            messageInformation.ExecuteError($"{windowsServiceName}发生意外故障,请在服务列表中,重启调度服务以确保配置生效");
                        } else {
                            control.Stop();
                            control.Start();
                            messageInformation.ExecuteSuccess($"{windowsServiceName}调度服务已重新启动");
                        }
                    }
                }
            } catch(Exception e) {
                messageInformation.ExecuteError($"{windowsServiceName}启动时,发生如下异常:\r" + e.Message);
            }

            return messageInformation;
        }
예제 #2
0
        /// <summary>
        /// 内部默认UTF-8编码
        /// </summary>
        /// <param name="uri">浏览器地址</param>
        /// <param name="contentType">Content-Type</param>
        /// <param name="bodyParameters">POST请求时:常用的为JSON,XML已经在外部处理好</param>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>执行结果</returns>
        public static MessageInformation PostWithCredential(string uri, string contentType, string bodyParameters, string userName, string password)
        {
            var messageInformation = new MessageInformation();

            try {
                uri = $"{uri}?{bodyParameters}";
                var webRequest = WebRequest.Create(uri);
                webRequest.ContentType = contentType;
                webRequest.Method      = "POST";
                CredentialCache mycache = new CredentialCache {
                    { webRequest.RequestUri, "Basic", new NetworkCredential(userName, password) }
                };
                webRequest.Credentials = mycache;
                byte[] bytes = Encoding.UTF8.GetBytes(bodyParameters);
                webRequest.ContentLength = bytes.Length;
                using (Stream reqStream = webRequest.GetRequestStream()) {
                    reqStream.Write(bytes, 0, bytes.Length);
                }
                HttpWebResponse httpWebResponse = webRequest.GetResponse() as HttpWebResponse;
                if (httpWebResponse != null)
                {
                    messageInformation.ExecuteStatusCode((int)httpWebResponse.StatusCode, httpWebResponse.StatusDescription);
                }
                using (WebResponse response = webRequest.GetResponse()) {
                    // ReSharper disable once AssignNullToNotNullAttribute
                    using (var streamReader = new StreamReader(response.GetResponseStream())) {
                        messageInformation.ExecuteSuccess(streamReader.ReadToEnd());
                        messageInformation.ExecuteResponseMessage(streamReader.ReadToEnd());
                    }
                }
            } catch (WebException ex) {
                HttpWebResponse httpWebResponse = ex.Response as HttpWebResponse;
                if (httpWebResponse != null)
                {
                    messageInformation.ExecuteStatusCode((int)httpWebResponse.StatusCode, httpWebResponse.StatusDescription);
                }
                if (ex.Response?.GetResponseStream() != null)
                {
                    // ReSharper disable once AssignNullToNotNullAttribute
                    using (var streamReader = new StreamReader(ex.Response.GetResponseStream())) {
                        messageInformation.ExecuteResponseMessage(streamReader.ReadToEnd());
                    }
                }
                messageInformation.ExecuteError(ex.Message);
            } catch (Exception ex) {
                messageInformation.ExecuteError(ex.Message);
            }
            return(messageInformation);
        }
예제 #3
0
        /// <summary>
        /// Soap协议Post方法,且需要oData身份认证
        /// </summary>
        /// <param name="uri">WebService地址</param>
        /// <param name="body">传入Soap协议格式数据</param>
        /// <param name="userName">用户名</param>
        /// <param name="password">密码</param>
        /// <returns>执行结果</returns>
        public static MessageInformation PostWebServiceWithCredential(string uri, string body, string userName, string password)
        {
            var messageInformation = new MessageInformation();

            try {
                var             webRequest = WebRequest.Create(uri);
                CredentialCache mycache    = new CredentialCache {
                    { webRequest.RequestUri, "Basic", new NetworkCredential(userName, password) }
                };
                webRequest.Credentials = mycache;
                webRequest.ContentType = "text/xml; charset=utf-8";
                webRequest.Method      = "POST";
                webRequest.Timeout     = int.MaxValue;
                using (Stream requestStream = webRequest.GetRequestStream()) {
                    byte[] paramBytes = Encoding.UTF8.GetBytes(body);
                    requestStream.Write(paramBytes, 0, paramBytes.Length);
                }
                HttpWebResponse httpWebResponse = webRequest.GetResponse() as HttpWebResponse;
                if (httpWebResponse != null)
                {
                    messageInformation.ExecuteStatusCode((int)httpWebResponse.StatusCode, httpWebResponse.StatusDescription);
                }
                WebResponse webResponse = webRequest.GetResponse();
                // ReSharper disable once AssignNullToNotNullAttribute
                using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) {
                    messageInformation.ExecuteSuccess(streamReader.ReadToEnd());
                    messageInformation.ExecuteResponseMessage(streamReader.ReadToEnd());
                }
            } catch (WebException ex) {
                HttpWebResponse httpWebResponse = ex.Response as HttpWebResponse;
                if (httpWebResponse != null)
                {
                    messageInformation.ExecuteStatusCode((int)httpWebResponse.StatusCode, httpWebResponse.StatusDescription);
                }
                if (ex.Response?.GetResponseStream() != null)
                {
                    // ReSharper disable once AssignNullToNotNullAttribute
                    using (var streamReader = new StreamReader(ex.Response.GetResponseStream())) {
                        messageInformation.ExecuteResponseMessage(streamReader.ReadToEnd());
                    }
                }
                messageInformation.ExecuteError(ex.Message);
            } catch (Exception ex) {
                messageInformation.ExecuteError(ex.Message);
            }
            return(messageInformation);
        }
예제 #4
0
        /// <summary>
        /// 内部默认UTF-8编码
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="parameters">parameter1=12&amp;parameter2='参数2'</param>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns>执行结果</returns>
        public static MessageInformation GetWithCredential(string uri, string parameters, string userName, string password)
        {
            var messageInformation = new MessageInformation();

            try {
                uri = $"{uri}?{parameters}";
                var webRequest = WebRequest.Create(uri);
                webRequest.Method = "GET";
                CredentialCache mycache = new CredentialCache {
                    { webRequest.RequestUri, "Basic", new NetworkCredential(userName, password) }
                };
                webRequest.Credentials = mycache;
                HttpWebResponse httpWebResponse = webRequest.GetResponse() as HttpWebResponse;
                if (httpWebResponse != null)
                {
                    messageInformation.ExecuteStatusCode((int)httpWebResponse.StatusCode, httpWebResponse.StatusDescription);
                }
                using (WebResponse response = webRequest.GetResponse()) {
                    // ReSharper disable once AssignNullToNotNullAttribute
                    using (var streamReader = new StreamReader(response.GetResponseStream())) {
                        messageInformation.ExecuteSuccess(streamReader.ReadToEnd());
                        messageInformation.ExecuteResponseMessage(streamReader.ReadToEnd());
                    }
                }
            } catch (WebException ex) {
                HttpWebResponse httpWebResponse = ex.Response as HttpWebResponse;
                if (httpWebResponse != null)
                {
                    messageInformation.ExecuteStatusCode((int)httpWebResponse.StatusCode, httpWebResponse.StatusDescription);
                }
                if (ex.Response?.GetResponseStream() != null)
                {
                    // ReSharper disable once AssignNullToNotNullAttribute
                    using (var streamReader = new StreamReader(ex.Response.GetResponseStream())) {
                        messageInformation.ExecuteResponseMessage(streamReader.ReadToEnd());
                    }
                }
                messageInformation.ExecuteError(ex.Message);
            } catch (Exception ex) {
                messageInformation.ExecuteError(ex.Message);
            }
            return(messageInformation);
        }
예제 #5
0
 /// <summary>
 /// 停止Windows服务
 /// </summary>
 /// <param name="windowsServiceName">服务名称</param>
 public static MessageInformation StopWindowsService(string windowsServiceName) {
     var messageInformation = new MessageInformation();
     try {
         var isNotExists = ServiceController.GetServices().All(data => data.ServiceName != windowsServiceName);
         if(isNotExists)
             throw new Exception($"Windows服务{windowsServiceName}不存在");
         using(ServiceController control = new ServiceController(windowsServiceName)) {
             if(control.Status == ServiceControllerStatus.Running) {
                 control.Stop();
                 messageInformation.ExecuteSuccess($"{windowsServiceName}调度服务已正确关闭");
             } else if(control.Status == ServiceControllerStatus.Stopped) {
                 messageInformation.ExecuteError($"{windowsServiceName}调度服务已经处于关闭当中");
             }
             control.WaitForStatus(ServiceControllerStatus.Stopped);
         }
     } catch(Exception e) {
         messageInformation.ExecuteError($"{windowsServiceName}停止时,发生如下异常:\r" + e.Message);
     }
     return messageInformation;
 }