定义一个WebException public class WebException extends RuntimeException { private int code; private String errorMsg; public WebException(int code, String errorMsg) { super(errorMsg); this.code = code; this.errorMsg 在异常捕获器添加WebException异常捕获 @ExceptionHandler(WebException.class) public BaseResult handleWebException(WebException e) { return new BaseResult(e.getCode(), e.getErrorMsg(), null); } 复制代码 模拟一段业务逻辑,抛出WebException 在之前的 Object deleteUser(@PathVariable(value = "userId") Integer userId) { if (userId == 0) { throw new WebException
(e is WebException) && ! (e is SecurityException)) { throw new WebException("net_webclient", e); } throw; } } } internal void (e is WebException) && ! (e is WebException) && ! (exception1 is WebException) && !
这就造成了当使用HttpWebRequest访问时,不成功就会出现System.Net.WebException: ‘The remote server returned an error: (500) { res = (HttpWebResponse)webRequest.GetResponse(); } catch (WebException
request.GetResponse(); request.GetRequestStream().Close(); } catch (WebException response = (HttpWebResponse)request.GetResponse(); } catch (WebException
httpResult, httpWebResponse); 40 httpWebResponse.Close(); 41 } 42 } 43 catch (WebException webException) 44 { 45 GetWebExceptionResponse(ref httpResult, webException); 46 } 47 4 /// <param name="httpResult">即将被HTTP请求封装函数返回的HttpResult变量</param> 5 /// <param name="<em>webException</em> ">访问网络期间发生错误时引发的异常对象</param> 6 private void GetWebExceptionResponse(ref HttpResult httpResult, WebException webException) 7 { 8 HttpWebResponse exResponse = webException.Response as HttpWebResponse; 9
httpResult, httpWebResponse); 41 httpWebResponse.Close(); 42 } 43 } 44 catch (WebException webException) 45 { 46 GetWebExceptionResponse(ref httpResult, webException); 47 } 48 httpResult, httpWebResponse); 42 httpWebResponse.Close(); 43 } 44 } 45 catch (WebException webException) 46 { 47 GetWebExceptionResponse(ref httpResult, webException); 48 } 49
httpResult, httpWebResponse); 44 httpWebResponse.Close(); 45 } 46 } 47 catch (WebException webException) 48 { 49 GetWebExceptionResponse(ref httpResult, webException); 50 } 51
该HttpWebRequest的类会引发引发WebException同时访问资源时发生错误。该WebException.Status属性包含一个WebExceptionStatus值,指示错误的来源。 当WebException.Status是WebExceptionStatus.ProtocolError时,Response属性包含从资源接收的HttpWebResponse。 webException) 43 { 44 GetWebExceptionResponse(ref httpResult, webException); 45 } 46 ">访问网络期间发生错误时引发的异常对象</param> 6 private void GetWebExceptionResponse(ref HttpResult httpResult, WebException webException) 7 { 8 HttpWebResponse exResponse = webException.Response as HttpWebResponse; 9
httpResult, httpWebResponse); 42 httpWebResponse.Close(); 43 } 44 } 45 catch (WebException webException) 46 { 47 GetWebExceptionResponse(ref httpResult, webException); 48 } 49
httpResult, httpWebResponse); 34 httpWebResponse.Close(); 35 } 36 } 37 catch (WebException webException) 38 { 39 GetWebExceptionResponse(ref httpResult, webException); 40 } 41
request.GetResponse(); request.GetRequestStream().Close(); } catch (WebException request.GetResponse(); request.GetRequestStream().Close(); } catch (WebException
httpResult, httpWebResponse); 42 httpWebResponse.Close(); 43 } 44 } 45 catch (WebException webException) 46 { 47 GetWebExceptionResponse(ref httpResult, webException); 48 } 49
requestStream.Write(bufferBytes, 0, bufferBytes.Length); } } catch (WebException response = request.GetResponse() as HttpWebResponse; } catch (WebException
httpResult, httpWebResponse); 42 httpWebResponse.Close(); 43 } 44 } 45 catch (WebException webException) 46 { 47 GetWebExceptionResponse(ref httpResult, webException); 48 } 49 , httpWebResponse); 177 httpWebResponse.Close(); 178 } 179 } 180 catch (WebException webException) 181 { 182 GetWebExceptionResponse(ref httpResult, webException); 183 }
response = (HttpWebResponse)request.GetResponse(); } catch (WebException webException) { response = (HttpWebResponse)webException.Response;
Server stack trace: 在 System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason) 在 System.ServiceModel.Channels.HttpChannelFactory
在回复状态为“304 (Not Modified)”的情况下,GetResponse方法会 抛出一个WebException异常,所以我们对该类型的异常进行的捕获。 如果WebException异常的StatusCode属性返回的HTTP状态是我们预知的NotModified,则意味着获取的员工列表未曾改变,于是我们在控制台上打印“服务端数据未发生变化”字样。 Console.WriteLine(reader.ReadToEnd() + Environment.NewLine); 19: } 20: } 21: catch (WebException
httpResult, httpWebResponse); 24 httpWebResponse.Close(); 25 } 26 } 27 catch (WebException webException) 28 { 29 GetWebExceptionResponse(ref httpResult, webException); 30 } 31 httpResult, httpWebResponse); 39 httpWebResponse.Close(); 40 } 41 } 42 catch (WebException webException) 43 { 44 GetWebExceptionResponse(ref httpResult, webException); 45 } 46
通过如下代码可以拿到错误信息 HttpWebResponse res; try { res = (HttpWebResponse)req.GetResponse(); } catch (WebException
id=aiclass&localVersion=0.1.0&arch=amd64: System.Net.WebException: 远程服务器返回错误: (403) 已禁止。 id=aiclass&localversion=0.1.0&arch=amd64: System.Net.WebException: 远程服务器返回错误: (403) 已禁止。 <LogIfThrows>d__43`1.MoveNext() 2021-04-25 14:51:43> CheckForUpdateImpl: Download resulted in WebException (returning blank release list): System.Net.WebException: 远程服务器返回错误: (403) 已禁止。