简体   繁体   English

GetResponse()上500内部服务器错误

[英]500 internal server error at GetResponse()

I have a heavy traffic aspx page calling a web service upon every user`s request as follows. 我有一个繁重的流量aspx页面根据每个用户的请求调用Web服务,如下所示。

string uri = "Path.asmx";
string soap = "soap xml string";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Headers.Add("SOAPAction", "\"http://xxxxxx"");
request.ContentType = "text/xml;charset=\"utf-8\"";
request.Accept = "text/xml";
request.Method = "POST";

using (Stream stm = request.GetRequestStream())
{
    using (StreamWriter stmw = new StreamWriter(stm))
    {
        stmw.Write(soap);
    }
}
WebResponse response = request.GetResponse();
response.close();

Everything is working fine but sometimes I am getting the following error. 一切都很好,但有时我得到以下错误。

The remote server returned an error: (500) Internal Server Error. 远程服务器返回错误:(500)内部服务器错误。 at System.Net.HttpWebRequest.GetResponse() 在System.Net.HttpWebRequest.GetResponse()

Does anybody have any idea about this error or can anybody tell me if I am doing wrong. 有没有人对这个错误有任何想法,或者有人告诉我,如果我做错了。

For me this error occurred because I had 2 web API actions that had the exact same signatures and both had the same verbs, HttpPost , what I did was change one of the verbs (the one used for updating) to PUT and the error was removed. 对我来说,这个错误发生是因为我有2个Web API动作具有完全相同的签名,并且都有相同的动词, HttpPost ,我所做的是将其中一个动词(用于更新的动词)更改为PUT并且错误被删除。 The following in my catch statement helped in getting to the root of the problem: 我的catch语句中的以下内容有助于找到问题的根源:

catch (WebException webex)
{
                WebResponse errResp = webex.Response;
                using (Stream respStream = errResp.GetResponseStream())
                {
                    StreamReader reader = new StreamReader(respStream);
                    string text = reader.ReadToEnd();
                }
}

From that error, I would say that your code is fine, at least the part that calls the webservice. 从那个错误,我会说你的代码很好,至少是调用webservice的部分。 The error seems to be in the actual web service. 该错误似乎在实际的Web服务中。

To get the error from the web server, add a try catch and catch a WebException. 要从Web服务器获取错误,请添加try catch并捕获WebException。 A WebException has a property called Response which is a HttpResponse. WebException有一个名为Response的属性,它是一个HttpResponse。 you can then log anything that is returned, and upload you code. 然后,您可以记录返回的任何内容,并上传代码。 Check back later in the logs and see what is actually being returned. 稍后在日志中查看并查看实际返回的内容。

Have you tried to specify UserAgent for your request? 您是否尝试为您的请求指定UserAgent? For example: 例如:

request.UserAgent = "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)";

Finally I get rid of internal server error message with the following code. 最后,我用以下代码摆脱了内部服务器错误消息。 Not sure if there is another way to achieve it. 不确定是否有其他方法可以实现它。


string uri = "Path.asmx";
string soap = "soap xml string";

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Headers.Add("SOAPAction", "\"http://xxxxxx"");
request.ContentType = "text/xml;charset=\"utf-8\"";
request.Accept = "text/xml";
request.Method = "POST";

using (Stream stm = request.GetRequestStream())
{
    using (StreamWriter stmw = new StreamWriter(stm))
    {
        stmw.Write(soap);
    }
}
 using (WebResponse webResponse = request.GetResponse()) { } 

首先查看您的错误消息我建议您重新编译整个应用程序,确保在重新编译时所有必需的dll都在bin文件夹中。

In my case my request object inherited from base object. 在我的情况下,我的请求对象继承自基础对象。 Without knowingly I added a property with int? 没有明知我用int添加了一个属性? in my request object and my base object also has same property ( same name ) with int datatype. 在我的请求对象中,我的基础对象也具有与int数据类型相同的属性(同名)。 I noticed this and deleted the property which I added in request object and after that it worked fine. 我注意到这一点并删除了我在请求对象中添加的属性,之后它工作正常。

For me the error was misleading. 对我来说,错误是误导。 I discovered the true error by testing the errant web service with SoapUI. 我通过使用SoapUI测试错误的Web服务发现了真正的错误。

In my case I just remove the SoapAction instruction from the HttpWebRequest object. 在我的例子中,我只是从HttpWebRequest对象中删除SoapAction指令。 So, I don't define .Headers.Add("SOAPAction","someurl") in HttpWebRequest definitions and my code works fine. 所以,我没有在HttpWebRequest定义中定义.Headers.Add("SOAPAction","someurl") ,我的代码运行正常。

ResultXML is an XDocument . ResultXML是一个XDocument ResultString is a string. ResultString是一个字符串。

try
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(Url);
    //req.Headers.Add("SOAPAction", "http://tempuri.org/IWebService/GetMessage");
    req.ProtocolVersion = HttpVersion.Version11;
    req.ContentType = "text/xml;charset=\"utf-8\"";
    req.Accept = "text/xml";
    req.KeepAlive = true;
    req.Method = "POST";        

    using (Stream stm = req.GetRequestStream())
    {
        using (StreamWriter stmw = new StreamWriter(stm))
            stmw.Write(soapStr);
    }
    using (StreamReader responseReader = new StreamReader(req.GetResponse().GetResponseStream()))
    {
        string result = responseReader.ReadToEnd();
        ResultXML = XDocument.Parse(result);
        ResultString = result;      
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM