I am wanting to sequentially send POST()
data to a website. I want post1 to finish, then begin post2, then begin post3 etc etc. I have syntax that compiles, but I get an error of that I am unsure how to overcome.
AggregateException was caught
One or more errors occured
Here is my syntax I use:
private multipost()
{
string postsite = string.Format("http://requestb.in/1g5y7234");
string postData = "";
string responseMessage = "";
DataTable grds = new DataTable();
grds.Columns.Add("Dosage", typeof(int));
grds.Columns.Add("Drug", typeof(string));
grds.Columns.Add("Patient", typeof(string));
grds.Columns.Add("Date", typeof(DateTime));
grds.Rows.Add(25, "Indocin", "David", DateTime.Now);
grds.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
grds.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
grds.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
grds.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);
try
{
Parallel.ForEach(grds.Rows.OfType<DataRow>(), row =>
{
foreach (var item in row.ItemArray)
{
postData += "=" + item + "&";
postData += "d2p=";
System.Net.HttpWebRequest r = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(postsite);
r.Method = "POST";
r.Accept = "application/json";
r.ContentType = "application/x-www-form-urlencoded";
r.ContentLength = postData.Length;
r.KeepAlive = false;
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(r.GetRequestStream()))
{
try { sw.Write(postData); }
catch (Exception ex) { responseMessage = ex.Message; }
}
var response = r.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
var result = readStream.ReadToEnd();
var xml = System.Xml.Linq.XElement.Parse(result);
if (xml.Elements("success").FirstOrDefault().Value == "1") { responseMessage = "Success"; }
else
{
var errors = xml.Elements("errors");
foreach (var error in errors.Elements("error")) { responseMessage = error.Value; }
}
}
});
}
catch (Exception mbop) { throw mbop; }
}
Looks like I forgot to actually pose a question to the powerful minds. What is causing this error, what do I do to overcome this error?
EDIT
I altered my catch{}
block to
catch (Exception mbop)
{
while (mbop.InnerException != null)
throw mbop;
}
And the InnerException
it produced is
Data at the root level is invalid. Line 1, position 1