简体   繁体   中英

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

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");
    }
}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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