访问服务时出错引发的异常。
상속: System.Exception
예제 #1
0
 /// <summary>
 /// 构造函数。
 /// </summary>
 /// <param name="exception">异常信息。</param>
 public ServiceErrorEventArgs(ServiceException exception)
 {
     this._exception = exception;
 }
        private static Stream GetRequestStream(string url, HttpRequestMethod method, string postData)
        {
            if (System.Windows.Deployment.Current.Dispatcher.CheckAccess())
            {
                throw new InvalidOperationException("不能在UI线程上执行此方法。");
            }
            HttpWebRequest httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
            httpWebRequest.Method = method.ToString();
            switch (method)
            {
                case HttpRequestMethod.GET:
                    break;
                case HttpRequestMethod.POST:
                case HttpRequestMethod.PUT:
                    UTF8Encoding encoding = new UTF8Encoding();
                    httpWebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                    byte[] data = encoding.GetBytes(postData);
                    AutoResetEvent requestWaitHandler = new AutoResetEvent(false);
                    AsyncCallback requestCallback = ar =>
                    {
                        Stream stream = httpWebRequest.EndGetRequestStream(ar);
                        stream.Write(data, 0, data.Length);
                        stream.Close();
                        requestWaitHandler.Set();
                    };
                    IAsyncResult asyncRequestResult = httpWebRequest.BeginGetRequestStream(requestCallback, null);
                    requestWaitHandler.WaitOne();
                    break;
                default:
                    break;
            }
            AutoResetEvent responseWaitHandler = new AutoResetEvent(false);
            Exception exception = null;
            Stream resultStream = null;
            AsyncCallback responseCallback = ar =>
            {
                try
                {
                    HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.EndGetResponse(ar);
                    resultStream = httpWebResponse.GetResponseStream();
                }
                catch (WebException webException)
                {
                    HttpWebResponse errorResponse = webException.Response as HttpWebResponse;
                    if (errorResponse == null)
                    {
                        exception = new ServiceException(webException.Message, (int)(webException.Status), webException);
                    }
                    //当资源不存在时,tomcat返回404错误,错误流为html类型,故处理
                    if (errorResponse.ContentType.IndexOf("text/html") >= 0)
                    {
                        exception = new ServiceException(webException.Message, (int)errorResponse.StatusCode, webException);
                    }
                    Stream errorStream = errorResponse.GetResponseStream();
                    StreamReader reader = new StreamReader(errorStream);
                    string errorMessage = reader.ReadToEnd();
                    ErrorResource errorResource = JsonConvert.DeserializeObject<ErrorResource>(errorMessage);

                    errorResponse.Close();
                    errorStream.Close();
                    reader.Close();
                    exception = new SuperMap.Connector.Utility.ServiceException(errorResource.Error.ErrorMsg, errorResource.Error.Code, webException);
                }
                finally
                {
                    responseWaitHandler.Set();
                }
            };
            IAsyncResult asyncResponseResult = httpWebRequest.BeginGetResponse(responseCallback, null);
            responseWaitHandler.WaitOne();
            if (exception != null) throw exception;
            return resultStream;
        }