c# - 在 Visual Studio 中显示不友好的 HTTP 500 错误

标签 c# visual-studio-2008 http

我正在编写一个将 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 was unhandled Message="The remote server returned an error: (500) Internal Server Error." Source="System" StackTrace: at System.Net.HttpWebRequest.GetResponse() at SOAP_Test.Program.Main(String[] args) in :line 41 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

最佳答案

捕获异常并显示消息和内部异常(如果可用)。

关于c# - 在 Visual Studio 中显示不友好的 HTTP 500 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3335723/

相关文章:

c# - VS 2005 工具箱类控件.NET

c# - 如何在 Database.EnsureCreated() EF Xamarin.Forms 之后使用 Database.Migration()

visual-studio - 将库添加到 Visual Studio 问题

c++ - 如何使用 boost::asio 在 http 上进行 POST?

c# - 创建带有捕获异常信息的 Windows 窗体?

c# - Entity Framework Core – “Insert if not exists” 可能吗?

asp.net - 如何关闭Visual Studio启动的Web服务

asp.net - 如何在 vs2008 中关闭 JScript Intellisense?

http - HTTP Header 或 Response Body 中的 Rest 错误消息?

java - 从服务异常中检索 Spring Integration 中的消息