0

使用下面的代码,我可以请求一个带有帖子参数的页面。但是请求的页面无法获取参数。我将两个文本框放入请求的页面并将参数发送到该页面。这段代码有什么问题?

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;
}
4

1 回答 1

1

确保目标页面上文本框输入的“名称”是Textbox1and Textbox2

于 2012-10-30T10:22:29.677 回答