Call to GetResponse() is throwing (500) Internal Server Error

独自空忆成欢 提交于 2019-12-25 07:32:24

问题


What is causing this exception to be thrown, and how could it be remedied

I am making an API Post to server, passing a serialized XML file, and I expect one for the return call.

Instead my code is throwing a 500 Internal Server Error at the GetResponse() call.

I have checked the syntax of my file, and it is correct. I also know that my logic works as I have used it for other calls.

try
    {                       
        wWebRequest = (HttpWebRequest)WebRequest.Create(strWebAddressAuthentication);
        wWebRequest.CookieContainer = new CookieContainer();
        wWebRequest.Method = sHTTPTransMethod;
        wWebRequest.ServicePoint.Expect100Continue = false;
        wWebRequest.UserAgent = sUserAgent;
        wWebRequest.ContentType = srequestContentType;

        FileInfo fInfo = new FileInfo(filename);
        long numBytes = fInfo.Length;
        double dLen = Convert.ToDouble(fInfo.Length / 1000000);

        if (dLen < 100)
        {
            fStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
            br = new BinaryReader(fStream);
            data = br.ReadBytes((int)numBytes);
            br.Close();
            wWebRequest.ContentLength = data.Length;                    
            dataStream = wWebRequest.GetRequestStream();
            dataStream.Write(data, 0, data.Length);
            dataStream.Close();

            response = (HttpWebResponse)wWebRequest.GetResponse();
            foreach (Cookie cook in response.Cookies)
            {
                myCookieContainer.Add(cook);
            }
            txtXmlReturned.Text = (((HttpWebResponse)response).StatusDescription);
            sServerResponse = txtXmlReturned.Text + Environment.NewLine;
            dataStream = response.GetResponseStream();
            reader = new StreamReader(dataStream);
            responseFromServer = reader.ReadToEnd();
            txtXmlReturned.Text = txtXmlReturned.Text + responseFromServer + Environment.NewLine;

            reader.Close();
            response.Close();
            fStream.Close();
            fStream.Dispose();
            // -----------------------------------------------------------
        }
        else
        {
            MessageBox.Show("The file selected exceeds the size limit for uploads.", "File Size");
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message.ToString(), "Upload Error");
    }
}

回答1:


A 500 Internal Server Error means there's a problem on the server side. You're writing for the client. See what happens if you go to the url you're using in a browser.




回答2:


For us, one of our apps stopped working and responded with a 500 code. Turned out the application pool crapped out. Restarting the site in question cleared it up. Of course, there could be some other underlying problem, but the app has been running for years and this is only the second time we've encountered a 500 with it. Nothing was logged by our app either.

Give IIS a check, assuming it is IIS and you have access to the server, and see if anything has gone astray there.



来源:https://stackoverflow.com/questions/17223771/call-to-getresponse-is-throwing-500-internal-server-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!