Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# webBrowser Cookies和表单提交_C#_Forms_Cookies_Browser - Fatal编程技术网

C# webBrowser Cookies和表单提交

C# webBrowser Cookies和表单提交,c#,forms,cookies,browser,C#,Forms,Cookies,Browser,我想导航到使用预定义cookie的网站, 向两个输入type=“text”中添加一些文本,然后使用提交按钮提交表单。 我知道这是可以做到的,但我不知道怎么做 我已经尝试将POST数据发送到页面,但我必须单击按钮才能执行操作。 这是我的密码: static String readHtmlPage(string url) { //setup some variables String username = "demo";

我想导航到使用预定义cookie的网站, 向两个
输入type=“text”
中添加一些文本,然后使用提交按钮提交表单。 我知道这是可以做到的,但我不知道怎么做

我已经尝试将POST数据发送到页面,但我必须单击按钮才能执行操作。 这是我的密码:

        static String readHtmlPage(string url)
        {

        //setup some variables

        String username = "demo";
        String password = "password";
        String firstname = "John";
        String lastname = "Smith";

        //setup some variables end

        String result = "";
        String strPost = "username=" + username + "&password=" + password + "&firstname=" + firstname + "&lastname=" + lastname;
        StreamWriter myWriter = null;

        HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url);
        objRequest.Headers["Cookie"] = "sid=0";
        objRequest.Headers["Cookie"] = "username=0";
        objRequest.Method = "POST";
        objRequest.ContentLength = strPost.Length;
        objRequest.ContentType = "application/x-www-form-urlencoded";

        try
        {
            myWriter = new StreamWriter(objRequest.GetRequestStream());
            myWriter.Write(strPost);
        }
        catch (Exception e)
        {
            return e.Message;
        }
        finally
        {
            myWriter.Close();
        }

        HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
        using (StreamReader sr =
           new StreamReader(objResponse.GetResponseStream()))
        {
            result = sr.ReadToEnd();

            // Close and clean up the StreamReader
            sr.Close();
        }
        return result;
    }


    static void Main(string[] args)
    {

        Console.Write(readHtmlPage("http://www.ggogle.com/"));
    }

我过去的建议是:
-使用Fiddler,用浏览器访问网站,像平常一样填写表格。
-Fiddler将记录请求/响应,您可以复制post数据字符串并替换其中需要的任何值,然后使用HttpWebRequest/HttpWebResponse以编程方式执行post并获取响应

示例:这是我在提交最后一条评论时捕获的帖子数据

comment=When+you+collect+the+recorded+POST+string+you+can+swap+out+the+key+value+pairs+in+there+before+you+make+the+request.+When+OnClick+event+fires+it+will+POST+data+to+the+server%2C+这是+you+需要+用+javascript+重新创建+nothing+的东西。&fkey=62a7d57a52ee7fa723413a2e1dbe7e71

string postData = string.format("comment={0}&fkey={1}", myCommentString, myFKey);
然后,您可以将此字符串传递给帖子所针对的url,并使用HttpWebRequest重新创建它


您还需要确保在post字符串中对您的值进行URL编码。

我需要每次发送不同的值。如果我发送POST数据,它将不起作用,因为该操作绑定到submit按钮的OnClick事件。当您收集记录的POST字符串时,您可以在发出请求之前交换其中的键值对。当OnClick事件触发时,它会将数据发布到服务器,这就是您需要使用javascript不重新创建任何内容的地方。:D任何时候,很高兴它有帮助