I have this update function that POSTs JSON data to a url and I got most of the POST parts from stack overflow questions. But I'm having a little trouble understanding what part of it means.
Here's my code:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow row = GridView1.Rows[e.RowIndex];
string startTime = row.Cells[1].Text;
string endTime = row.Cells[2].Text;
string furnace = row.Cells[4].Text;
long ID = Convert.ToInt64(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
CascadingDropDown cddSubsystem = (CascadingDropDown) row.FindControl("cddSubsystem");
CascadingDropDown cddReason = (CascadingDropDown) row.FindControl("cddReason");
DropDownList ddForcedSched = (DropDownList) row.FindControl("ddEditForcedSched");
TextBox txtComments = (TextBox) row.FindControl("txtEditOperatorComments");
// cascading dropdowns have the selected value formatted like this ReasonCode:::ReasonName
string[] reason = cddReason.SelectedValue.Split(new string[] {":::"}, StringSplitOptions.None);
string[] subsystem = cddSubsystem.SelectedValue.Split(new string[] {":::"}, StringSplitOptions.None);
// get the machine code
string machine = "";
foreach (SorEvent evt in _events)
if (evt.Id == ID)
machine = evt.MachineCode;
// create SorEvent object to post
SorEvent updateSorEvent = new SorEvent()
{
Id = ID,
Furnace = furnace,
StartTime = Convert.ToDateTime(startTime),
EndTime = endTime != " " ? Convert.ToDateTime(endTime) : (DateTime?) null,
MachineCode = machine,
ReasonCode = reason[0],
SubsystemCode = subsystem[0],
ForceScheduleFlag = ddForcedSched.SelectedValue,
OperatorComments = txtComments.Text,
};
// POST to url
string url = @"http://cannot.divulge:3020/this/URL/";
HttpWebRequest req = WebRequest.Create(new Uri(url)) as HttpWebRequest;
req.Accept = "application/json";
req.ContentType = "application/json";
req.Method = "POST";
// build a string with the params and encode it
string data = JsonConvert.SerializeObject(updateSorEvent);
ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(data);
// send the request
using (Stream post = req.GetRequestStream())
{
post.Write(bytes, 0, bytes.Length);
}
var response = req.GetResponse();
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
var content = sr.ReadToEnd();
// return to non-edit state
GridView1.EditIndex = -1;
SetData();
}
I understand most of the POSTing functionality, but I'm confused about the following:
var response = req.GetResponse();
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
var content = sr.ReadToEnd();
I know that the response variable gets the response from the request that I defined above and then I'm creating a stream from the response and reading it and setting it to a variable called content.
Is the purpose of that segment of code to show that the url POST was successful?
I know that
post.Write(bytes, 0, bytes.length);
is doing the actual post, so the code after it must be for confirmation.
This is where I got my code: POSTing JSON to URL via WebClient in C#