使用下面的代码,我可以请求包含帖子参数的页面。但是请求的页面无法获取参数。我将两个文本框放到请求的页面并将参数发送到此页面。这段代码有什么问题?
private string PostForm(string _targetUrl, string _parameter1, string _parameter2)
{
WebRequest request = WebRequest.Create(_targetUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
string postContent = string.Format("Textbox1={0}&Textbox2={1}", _parameter1, _parameter2);
byte[] postContentBytes = Encoding.ASCII.GetBytes(postContent);
request.ContentLength = postContentBytes.Length;
Stream writer = request.GetRequestStream();
writer.Write(postContentBytes, 0, postContentBytes.Length);
writer.Close();
HttpWebResponse testResponse = (HttpWebResponse)request.GetResponse();
if (!testResponse.StatusDescription.Equals("OK", StringComparison.InvariantCultureIgnoreCase))
{
Response.Write("Error");
}
StreamReader sr = new StreamReader(testResponse.GetResponseStream());
string returnvalue = sr.ReadToEnd();
return returnvalue;
}
答案 0 :(得分:1)
确保目标网页上文本框输入的“名称”为Textbox1
和Textbox2
。