我需要从 HttpWebResponse 获取 500 错误响应消息。
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
{
post_response = responseStream.ReadToEnd();
responseStream.Close();
}
谢谢!
[编辑]
使用浏览器打开此网址,
浏览器将打印响应消息“缺少参数:M_ID”。
现在,我想使用 asp.net 获取该响应消息
var post_string = "hello=hi";
HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create("https://www.sagepayments.net/web_services/vterm_extensions/transaction_processing.asmx/BANKCARD_PRIOR_AUTH_SALE");
objRequest.Method = "POST";
objRequest.ContentLength = post_string.Length;
objRequest.ContentType = "application/x-www-form-urlencoded";
// post data is sent as a stream
StreamWriter myWriter = null;
myWriter = new StreamWriter(objRequest.GetRequestStream());
myWriter.Write(post_string);
myWriter.Close();
// returned values are returned as a stream, then read into a string
try
{
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
using (StreamReader responseStream = new StreamReader(objResponse.GetResponseStream()))
{
post_response = responseStream.ReadToEnd();
responseStream.Close();
}
Response.Write(post_response);
}
catch(WebException e)
{
Response.Write("Error : "+e.Message);
Response.Write("<br /> Data : " + e.Data);
Response.Write("<br /> HelpLink : " + e.HelpLink);
Response.Write("<br /> InnerException : " + e.InnerException);
Response.Write("<br /> Response : " + e.Response);
Response.Write("<br /> Source : " + e.Source);
Response.Write("<br /> Status : " + e.Status);
Response.Write("<br /> TargetSite : " + e.TargetSite);
}
Result :
Error : The remote server returned an error: (500) Internal Server Error.
Data : System.Collections.ListDictionaryInternal
HelpLink :
InnerException :
Response : System.Net.HttpWebResponse
Source : System
Status : ProtocolError
TargetSite : System.Net.WebResponse GetResponse()
如何获取此消息?
缺少参数:M_ID。