I am attempting to send XML data to a remote ColdFusion server using the code below:
[HttpPost]
public ActionResult Index(Transmission t)
{
ViewBag.ErrorMessage = "";
ViewBag.OtherMessage = "";
ViewBag.ResponseHtml = "";
//set request properties
var request = (HttpWebRequest)WebRequest.Create("theUrl");
request.ContentType = "application/xml";
request.ProtocolVersion = HttpVersion.Version10;
request.Method = "POST";
request.Timeout = 60000;
//serialize the ViewModel
XmlSerializer ser = new XmlSerializer(typeof(Transmission));
XmlWriterSettings xws = new XmlWriterSettings();
XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", "");
xws.OmitXmlDeclaration = true; //omit the xml declaration line
Stream stream = new MemoryStream();
StringBuilder sb = new StringBuilder();
XmlWriter writer = XmlWriter.Create(sb, xws);
ser.Serialize(writer, t, ns);
using (StreamReader sr = new StreamReader(stream))
{
string line;
while ((line = sr.ReadLine()) != null)
{
sb.AppendLine(line);
}
byte[] postBytes = Encoding.UTF8.GetBytes(sb.ToString());
request.ContentLength = postBytes.Length;
try
{
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(postBytes, 0, postBytes.Length);
requestStream.Close();
}
using (var response = (HttpWebResponse)request.GetResponse())
{
ViewBag.ResponseHtml = response.ToString();
return View("Error"); //TODO: change this to success when I get the 500 error fixed
}
}
catch (Exception ex)
{
ViewBag.ErrorMessage = ex.Message;
request.Abort();
return View("Error");
}
}
}
I have no access to the ColdFusion administrator, as it is a third-party vendor. According to a developer on their end, they are able to see transactions in their database logs, but I am getting a 500 error on my end. Is there something in the code I've posted that I should change, or would this be due to some setting on their side? The only information that I'm able to get is that it is a 500: Internal Server Error
. When I put breakpoints in my code, the program throws an exception at this line: using (var response = (HttpWebResponse)request.GetResponse())
.
Any help would be greatly appreciated. I would be happy to provide any further information necessary.