开发者

Show Unfriendly HTTP 500 Error in Visual Studio

开发者 https://www.devze.com 2023-01-09 09:48 出处:网络
I am writing an app that posts XML to a webservice and am constantly greeted with a WebException claiming a HTTP 500 error on the server.Is there anyway to see the specifics of the error akin to the \

I am writing an app that posts XML to a webservice and am constantly greeted with a WebException claiming a HTTP 500 error on the server. Is there anyway to see the specifics of the error akin to the "unfriendly" error messages in Internet Explorer?

The "See details" button doesn't seem to list the exact server response I'm looking for.

Thanks.

Here's the code and the exact error message:

static void Main(string[] args)
    {   


        //create requester
        WebRequest request = WebRequest.Create("http://server.com/service");

        //create string of xml to transfer
        string xml = "<xml>so开发者_如何学Pythonme 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:


Catch the exception and display the message and the innerexception if available.

0

精彩评论

暂无评论...
验证码 换一张
取 消