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

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

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

最新更新