我不是那个"锐利"在C#中。不过我需要这段代码工作。我尝试了不同的东西,没有用。正在记录异常,它只会错过响应的主体。这意味着它只记录标题。
我怎样才能得到身体。以下是我所拥有的。 " messageFromServer"是我试图用于记录身体的东西(我在这里找到它:https://stackoverflow.com/questions/11828843/webexception-how-to-get-whole-response-with-a-body#=)。
甚至尝试使用此处提出的内容:C# - Getting the response body from a 403 error。
catch (WebException ex)
{
var resp = (HttpWebResponse)ex.Response;
Console.WriteLine("The transaction failed. Please see the logfile for full errorlog");
File.AppendAllText("log_createtrxns.txt", "\r\nBOL\r\nDate" + headerDate + "\r\n Server body" + ex.Response + "\r\nAmount: " + decimalAmount + "\r\nData:" + ex.Data + "\r\nException: " + ex.Message + "\r\nSource: " + ex.Source + "\r\nStacktrace: " + ex.StackTrace + "\r\nEOL");
}
这只在我的日志文件中给了我这个:
服务器bodySystem.Net.HttpWebResponse
很抱歉,但我根本不明白它应该如何协同工作。
try
{
response = (HttpWebResponse)httpWebRequest.GetResponse();
foreach (string headerName in response.Headers)
{
//Console.WriteLine(headerName + ": " + response.Headers[headerName]);
}
var resp = new StreamReader(response.GetResponseStream()).ReadToEnd();
var messageFromServer = resp;
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
string result = readStream.ReadToEnd();
Console.WriteLine(result);
statusCode = (int) response.StatusCode;
if (statusCode == 201)
{
String location = response.Headers["Location"];
transId = Convert.ToInt32(location.Substring(location.LastIndexOf("/") + 1));
Console.WriteLine("Transaction ID: " + transId);
}
}
catch (Exception e)
{
Console.WriteLine("The transaction failed. Please see the logfile for full errorlog");
File.AppendAllText("log_createtrxns.txt", "\r\nBOL\r\nDate" + headerDate + "\r\n Server body" + messageFromServer + "\r\nAmount: " + decimalAmount + "\r\nData:" + e.Data + transId + "\r\nException: " + e.Message + "\r\nSource: " + e.Source + "\r\nStacktrace: " + e.StackTrace + "\r\nEOL");
}