我对使用 httpwebrequest 有点困惑。我试图查看一些文章,但由于我是第一次这样做,所以无法从中获得太多。下面是我正在尝试处理的代码,我有几个问题,
a) ASPX 页面定义了一些控件,并且在代码隐藏中我创建了一些控件。当我使用 POST 执行 httpwebrequest 时,是否需要考虑所有控件及其值?我只需要为其中一个控件执行 POST。我可以只为那个控制做吗?
b) 在“(HttpWebRequest)WebRequest.Create”中应该指定什么 URL?我假设它是向用户显示的同一页面。例如,在下面的示例中,它是 ("http://localhost/MyIdentity/Confirm.aspx?ID=New&Designer=True&Colors=Yes");
c) 还有什么我需要在标记或代码中修改或处理以实现 httpwebrequest 吗?
private void OnPostInfoClick(object sender, System.EventArgs e)
{
ASCIIEncoding encoding = new ASCIIEncoding();
string postData = ""; //Read from the stored array and print each element from the array loop here.
byte[] data = encoding.GetBytes(postData);
// Prepare web request...
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://localhost/MyIdentity/Confirm.aspx?ID=New&Designer=True&Colors=Yes");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
// Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
}