是什么导致此异常被抛出,以及如何补救
我正在向服务器发送一个 API Post,传递一个序列化的 XML 文件,我希望有一个用于返回调用。
相反,我的代码在 GetResponse() 调用中引发了 500 Internal Server Error。
我检查了我的文件的语法,它是正确的。我也知道我的逻辑可以正常工作,因为我已将其用于其他调用。
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");
}
}