我想在从数据库表中读取记录后向网站发送多条记录。我可以发送一条记录,但不能循环记录并一次发送一条记录。
这是我正在使用的代码的一部分:
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
SqlCommand cmd = new SqlCommand("select top 10 field1, field2, field3, field4 prices", conn);
SqlDataReader rd = cmd.ExecuteReader();
WebResponse rs;
string strNewValue;
StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
while (rd.Read())
{
strNewValue = "field1='" + rd[0].ToString() + "'&field2='" + rd[1].ToString() + "'&field3='AM'&field4=" + rd[2].ToString();
stOut.Write(strNewValue);
}
stOut.Close();
rs = req.GetResponse();
此代码仅发布最后一条记录。我怎样才能遍历记录并一次发布一个记录。
提前致谢
答案 0 :(得分:2)
看起来您将所有记录放入同一个http帖子中 - 尝试将其更改为发布每条记录 - 如下所示:
SqlCommand cmd = new SqlCommand("select top 10 field1, field2, field3, field4 prices", conn);
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
WebResponse rs; string strNewValue;
StreamWriter stOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
strNewValue = "field1='" + rd[0].ToString() + "'&field2='" + rd[1].ToString() + "'&field3='AM'&field4=" + rd[2].ToString();
stOut.Write(strNewValue);
stOut.Close();
rs = req.GetResponse();
}
rd.Close();