【发布时间】:2011-03-21 03:03:15
【问题描述】:
我正在编写一个将 XML 发布到 Web 服务的应用程序,并且经常收到一个 WebException,声称服务器上出现 HTTP 500 错误。无论如何,是否可以查看类似于 Internet Explorer 中“不友好”错误消息的错误细节?
“查看详细信息”按钮似乎没有列出我正在寻找的确切服务器响应。
谢谢。
这是代码和确切的错误消息:
static void Main(string[] args)
{
//create requester
WebRequest request = WebRequest.Create("http://server.com/service");
//create string of xml to transfer
string xml = "<xml>some xml goes here</xml>";
//convert string to byte array
byte[] transfer = Encoding.ASCII.GetBytes(xml);
//set up method
request.Method = "POST";
//set up content type
request.ContentType = "text/xml";
//set up content length
request.ContentLength = transfer.Length;
//open data stream
Stream myStream = request.GetRequestStream();
//send the data
myStream.Write(transfer, 0, transfer.Length);
//Create object to capture response
WebResponse response = request.GetResponse();
//Create object to convert response to readable form
StreamReader reader = new StreamReader(response.GetResponseStream());
//Reach each line of the response stream and display on command line
string str = reader.ReadLine();
while(str != null)
{
Console.WriteLine(str);
str = reader.ReadLine();
}
Console.ReadLine();
}
System.Net.WebException 未处理 Message="远程服务器返回错误:(500) 内部服务器错误。" 来源="系统" 堆栈跟踪: 在 System.Net.HttpWebRequest.GetResponse() 在 SOAP_Test.Program.Main(String[] args) 中:第 41 行 在 System.AppDomain._nExecuteAssembly(程序集程序集,字符串 [] 参数) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,String [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() 内部异常:
【问题讨论】:
-
你在使用 HttpWebRequest 类吗?
-
你控制网络服务吗?如果它是您在某处调用的黑匣子,那么您对发回的内容(如果有的话)没有太多控制权。如果您控制服务,您能否发布有关如何处理错误的代码?
-
我正在使用 HttpWebRequest 类。不幸的是,它是一个黑匣子。但我确实知道服务器应该响应什么。我已经用代码更新了我上面的帖子。
标签: c# visual-studio-2008 http