如何获取HttpWebRequest.GetResponse()失败时的错误信息

我正在启动一个HttpWebRequest,然后检索它的响应。 偶尔,我得到一个500(或至less5个##)的错误,但没有说明。 我可以控制两个端点,并希望接收端获得更多信息。 例如,我想将exception消息从服务器传递给客户端。 这可能使用HttpWebRequest和HttpWebResponse?

码:

try { HttpWebRequest webRequest = HttpWebRequest.Create(URL) as HttpWebRequest; webRequest.Method = WebRequestMethods.Http.Get; webRequest.Credentials = new NetworkCredential(Username, Password); webRequest.ContentType = "application/x-www-form-urlencoded"; using(HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse) { if(response.StatusCode == HttpStatusCode.OK) { // Do stuff with response.GetResponseStream(); } } } catch(Exception ex) { ShowError(ex); // if the server returns a 500 error than the webRequest.GetResponse() method // throws an exception and all I get is "The remote server returned an error: (500)." } 

任何帮助,将不胜感激。

这可能使用HttpWebRequest和HttpWebResponse?

你可以让你的Web服务器简单地将exception文本写入响应主体,然后将状态码设置为500.现在客户端在遇到500错误时会抛出一个exception,但你可以读取响应stream并获取消息的例外。

所以你可以捕获一个WebException ,如果从服务器返回一个非200的状态代码并读取它的主体,将会抛出这个exception:

 catch (WebException ex) { using (var stream = ex.Response.GetResponseStream()) using (var reader = new StreamReader(stream)) { Console.WriteLine(reader.ReadToEnd()); } } catch (Exception ex) { // Something more serious happened // like for example you don't have network access // we cannot talk about a server exception here as // the server probably was never reached } 

当试图检查FTP站点上是否存在文件时,我遇到了这个问题。 如果文件不存在,则在尝试检查其时间戳时会出现错误。 但是我想通过检查它的types来确定错误不是别的。

WebException上的Response属性将是FtpWebResponsetypes,您可以在其中查看其StatusCode属性以查看您具有哪个FTP错误 。

这是我结束的代码:

  public static bool FileExists(string host, string username, string password, string filename) { // create FTP request FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + host + "/" + filename); request.Credentials = new NetworkCredential(username, password); // we want to get date stamp - to see if the file exists request.Method = WebRequestMethods.Ftp.GetDateTimestamp; try { FtpWebResponse response = (FtpWebResponse)request.GetResponse(); var lastModified = response.LastModified; // if we get the last modified date then the file exists return true; } catch (WebException ex) { var ftpResponse = (FtpWebResponse)ex.Response; // if the status code is 'file unavailable' then the file doesn't exist // may be different depending upon FTP server software if (ftpResponse.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable) { return false; } // some other error - like maybe internet is down throw; } } 

我面临着类似的情况:

我正在尝试使用BasicHTTPBinding读取原始响应,以防HTTP错误消耗SOAP服务。

但是,使用GetResponseStream()读取响应时,得到错误:

stream不可读

所以,这个代码为我工作:

 try { response = basicHTTPBindingClient.CallOperation(request); } catch (ProtocolException exception) { var webException = exception.InnerException as WebException; var rawResponse = string.Empty; var alreadyClosedStream = webException.Response.GetResponseStream() as MemoryStream; using (var brandNewStream = new MemoryStream(alreadyClosedStream.ToArray())) using (var reader = new StreamReader(brandNewStream)) rawResponse = reader.ReadToEnd(); } 
 HttpWebRequest myHttprequest = null; HttpWebResponse myHttpresponse = null; myHttpRequest = (HttpWebRequest)WebRequest.Create(URL); myHttpRequest.Method = "POST"; myHttpRequest.ContentType = "application/x-www-form-urlencoded"; myHttpRequest.ContentLength = urinfo.Length; StreamWriter writer = new StreamWriter(myHttprequest.GetRequestStream()); writer.Write(urinfo); writer.Close(); myHttpresponse = (HttpWebResponse)myHttpRequest.GetResponse(); if (myHttpresponse.StatusCode == HttpStatusCode.OK) { //Perform necessary action based on response } myHttpresponse.Close();