如何使用C#提交Html表单 [英] How Do I Submit Html Form Using C#

查看:149
本文介绍了如何使用C#提交Html表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

HI All

这是我的代码片段



HI All
This is my code snippet

var request = (HttpWebRequest)WebRequest.Create("http://xx.xx.xx.xx/xx/xx/xxx/xxx.aspx");
           var data1 = Encoding.ASCII.GetBytes(Url);
           request.Method = "POST";
           request.ContentType = "application/x-www-form-urlencoded";
           request.ContentLength = data1.Length;

           using (var stream = request.GetRequestStream())
           {
               stream.Write(data1, 0, data1.Length);
           }
           var response = (HttpWebResponse)request.GetResponse();
           var responseString =
                    new StreamReader(response.GetResponseStream()).ReadToEnd();





我正在成功获得响应字符串

这是HTML表格





and am getting response string successfully
which is HTML form

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Raligare Health </title>
    <script language="javascript" type="text/javascript">
        function submitForm() {
            var elm = document.forms[0].elements[0];
            elm.parentNode.removeChild(elm);
            document.forms[0].method = "post";
            document.forms[0].action = "https://xxx.xxx.com/xxx/xxx.run";
            document.forms[0].submit();
        }	   
    </script>
</head>
<body onload="submitForm();">
    <form method="post" action="xxx.aspx" id="form2">
    <div class="aspNetHidden">
        <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTM1MzQyOTg2M2Rk" />
    </div>
    <div class="aspNetHidden">
        <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWDQKV8tWeBQKUwbXvDALZyp+SAgLBkpHwAwLd44Q6Atf/o+4DArCBl4kJAoDqn+8IAoaV9JsNAtnX/9QNArSO36MFAo3+jNEEAqiXqasB" />
    </div>
    <div>
        <input type="hidden" name="agentId" id="agentId" value="20008025" />
        <input type="hidden" name="quotationReferenceNum" id="quotationReferenceNum" value="0" />
        <input type="hidden" name="emailAddress" id="emailAddress" value="raj@yahoo.in" />
        <input type="hidden" name="mobileNum" id="mobileNum" value="9873598972" />
        <input type="hidden" name="productFamily" id="productFamily" value="HEALTH" />
        <input type="hidden" name="pCode" id="pCode" value="10001101" />
        <input type="hidden" name="sumInsured" id="sumInsured" value="005" />
        <input type="hidden" name="coverType" id="coverType" value="INDIVIDUAL" />
        <input type="hidden" name="numberOfAdult" id="numberOfAdult" value="0" />
        <input type="hidden" name="numberOfChildren" id="numberOfChildren" value="0" />
        <input type="hidden" name="ageGroupOfEldestMember" id="ageGroupOfEldestMember" value="25 - 35" />
        <input type="hidden" name="tenure" id="tenure" value="1" />
    </div>
    </form>
</body>
</html>





现在我必须提交此表格,此表格应该将我重定向到另一页。怎么做..?

can任何人帮助我..



Now i have to submit this form and this form should redirect me to another page.How to do it..?
can anybody help me..

推荐答案

我假设您尝试使用代码而不是浏览器来完成所有这些工作?



您必须将HTML加载到 XMLReader



通读XML找到表单元素和所有输入元素。



获取 action 表单元素的属性,因为这是您应该将表单发回的地址。



获取 value name 来自每个输入元素的属性。



然后使用action和name,value对来构建一个新的WebRequest对象。



然后提交web请求。
I assume you're trying to do all of this with code and not a browser?

You have to load the HTML into an XMLReader.

Read through the XML to find the form element and all the input elements.

Get the action attribute of the form element as this is the address to which you should post the form back to.

Get the value and name attributes from each of the input elements.

Then use the action and name, value pairs to build up a new WebRequest object.

Then submit the web request.


这篇关于如何使用C#提交Html表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆