POST后,从下一页获取源代码

时间:2012-01-30 23:17:23

标签: c#

我将数据发布到表单并且一切运行良好,但之后我需要在POST后输入数据后获取页面的源(html)。

这是可能的,如果是这样的话?

我正在使用此方法进行POST:

 string HttpPost(string uri, string parameters)
    {
        HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(uri);
        webRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.2; rv:9.0.1) Gecko/20100101 Firefox/9.0.1";
        webRequest.ContentType = "application/x-www-form-urlencoded";
        webRequest.Method = "POST";
        byte[] bytes = Encoding.ASCII.GetBytes(parameters);
        Stream os = null;
        try
        {
            webRequest.ContentLength = bytes.Length;   
            os = webRequest.GetRequestStream();
            os.Write(bytes, 0, bytes.Length);         
        }
        catch (WebException ex)
        {
            MessageBox.Show(ex.Message, "HttpPost: Request error",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            if (os != null)
            {
                os.Close();
            }
        }

        try
        { 
            WebResponse webResponse = webRequest.GetResponse();
            if (webResponse == null)
            { return null; }
            StreamReader sr = new StreamReader(webResponse.GetResponseStream());
            return sr.ReadToEnd().Trim();
        }
        catch (WebException ex)
        {
            MessageBox.Show(ex.Message, "HttpPost: Response error",
               MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        return null;
    }

1 个答案:

答案 0 :(得分:0)

您的意思是您想要服务器的响应吗?如果是,请阅读HttpWebRequest的文档,尤其是GetResponse方法:http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx

编辑:感谢您现在发布源代码。这是你需要的基本结构,所以我不清楚你的问题是什么......