当前位置:  开发笔记 > 编程语言 > 正文

以编程方式提交表单

如何解决《以编程方式提交表单》经验,为你挑选了0个好方法。

我试图以编程方式提交一个特定的表单,但我总是得到初始页面.我一定做错了什么或错过了什么.我发送会话cookie和一些POST数据,如viewState(我从初始请求解析)和SessionID(这是我在表单中更改的值,以便从其他年份获取数据).但在第二个请求中,我总是获得Session 899的数据,而不是我请求的数据:875.

这是使用的代码:,任何帮助都非常苛刻

retrieveEdmIndexForSession(875);

 protected string retrieveEdmIndexForSession(int sessionId) {

    CookieContainer cookies;
    HttpWebRequest oRequest;
    HttpWebResponse oResponse;
    Stream sw;
    StreamReader sr;
    string pageData;
string PathRemote = @"http://edmi.parliament.uk/EDMi/EDMList.aspx";


    /*
     * download the index page so we can get Cookies and ViewState from it.
     */
    oRequest = (HttpWebRequest)WebRequest.Create(PathRemote);
    oRequest.Method = "GET";
    oRequest.AllowAutoRedirect = true;
    oRequest.CookieContainer = new CookieContainer();
    oRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    oRequest.Referer = "http://edmi.parliament.uk/EDMi/EDMList.aspx";


    oResponse =(HttpWebResponse) oRequest.GetResponse();
    sr = new StreamReader(oResponse.GetResponseStream());

    pageData = sr.ReadToEnd();

    /*
     * extract view state from pageData.
     */
    string viewState = this.ExtractViewState(pageData);


    /*
    * lets submit the form with the parameters we want
    */
    oRequest = (HttpWebRequest)WebRequest.Create(PathRemote);
    oRequest.Method = "POST";
    oRequest.AllowAutoRedirect = true;
    oRequest.ContentType = "application/x-www-form-urlencoded";
    oRequest.CookieContainer = new CookieContainer();
    oRequest.CookieContainer.Add(oResponse.Cookies);
    oRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    oRequest.Referer = "http://edmi.parliament.uk/EDMi/EDMList.aspx";

    string postdata = "__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=" + viewState + "&_MenuCtrl%3AddlSession=" + sessionId + "&_MenuCtrl%3A_GoTo.x=57&_MenuCtrl%3A_GoTo.y=14&ddlStatus=1&ddlSortedBy=1";
    byte[] buffer = Encoding.UTF8.GetBytes(postdata);
    oRequest.ContentLength = buffer.Length;

    /*
     * Send post data into request stream first
     */
    sw = oRequest.GetRequestStream();
    sw.Write(buffer, 0, buffer.Length);
    sw.Flush();
    sw.Close();

    /*
     * Connect, send and get response
     */
    oResponse = (HttpWebResponse)oRequest.GetResponse();
    sr = new StreamReader(oResponse.GetResponseStream());

    //OnLogUpdated(1, "\r\nStatus Code: " + oResponse.StatusCode);
    //OnLogUpdated(1, "\r\nServer: " + oResponse.Server);

    pageData = sr.ReadToEnd();
    string result = getSessionId(pageData);
    //OnLogUpdated(1, "\r\nRestuls: [" + result + "]");
    //OnLogUpdated(1, "\r\nPage: [" + pageData + "]");


    return pageData;


}



private string ExtractViewState(string str)
{
    string viewState = "";
    string pattern = "(?<=__VIEWSTATE\" value=\")(?.*?)(?=\")";

    Match match = Regex.Match(str, pattern);

    if (match.Success)
    {
        viewState = match.Groups["val"].Value;
        viewState = HttpUtility.UrlEncodeUnicode(viewState);

    }

    return viewState;
}

protected string getSessionId(string str)
{
    string sessionId = string.Empty;

    str = str.Trim();

    string pattern = @"&SESSION=([^']+)'";

    Match match = Regex.Match(str, pattern, RegexOptions.IgnoreCase);
    if (match.Success)
    {
        sessionId = match.Groups[1].ToString(); ;

    }

    return sessionId;
}

这是.NET脚本发送的RAW请求.

POST /EDMi/EDMList.aspx HTTP/1.1 Content-Type:application/x-www-form-urlencoded Accept:text/html,application/xhtml + xml,application/xml; q = 0.9,/ ; q = 0.8 Referer: http://edmi.parliament.uk/EDMi/EDMList.aspx 用户代理:.NET Framework客户端主机:edmi.parliament.uk Cookie:ASP.NET_SessionId = k55fqarvx2oszp2wxhtrol45内容长度:2431期望:100-continue

__EVENTTARGET =&__ EVENTARGUMENT =&__ VIEWSTATE = dDwxMDgyNzIxNDQ2O3Q8O2w8aTwzPjs%2bO2w8dDw7bDxpPDE%2bO2k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FtZW5kbWVudHM7Pj47Pjs7Pjt0PHA8cDxsPFRleHQ7PjtsPDE7Pj47Pjs7Pjt0PHA8cDxsPFRleHQ7PjtsPDUwOz4%2bOz47Oz47Pj47Pj47Pj47Pj47bDxfTWVudUN0cmw6X0dvVG87Pj5NHcFbPBNzNuwxs7sYLdUE2omkjw%3D%3D&_MenuCtrl%3AddlSession = 875&_MenuCtrl%3A_GoTo.x = 57&_MenuCtrl%3A_GoTo.y = 14&ddlStatus = 1&ddlSortedBy = 1

这是IE发送的RAW请求:

POST /EDMi/EDMList.aspx HTTP/1.1接受:image/gif,image/jpeg,image/pjpeg,image/pjpeg,application/x-shockwave-flash,application/xaml + xml,application/vnd.ms-xpsdocument, application/x-ms-xbap,application/x-ms-application,application/vnd.ms-excel,application/vnd.ms-powerpoint,application/msword,/ Referer:http: //edmi.parliament.uk/EDMi /EDMList.aspx Accept-Language:en-gb User-Agent:Mozilla/4.0(兼容; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0 .04506.30; InfoPath.1; .NET CLR 3.0.04506.648; OfficeLiveConnector.1.3; OfficeLivePatch.0.0; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)内容类型:application/x-www-form-urlencoded接受-Encoding:gzip,deflate主持人:edmi.parliament.uk内容长度:2431连接:Keep-Alive Pragma:no-cache Cookie:WT_FPC = id = 83.217.99.254-2364242496.30021299:lv = 1249572414567:ss = 1249572414567; ASP.NET_SessionId = vwxgo4rlex1j5m55l0bivrqo

__EVENTTARGET =&__ EVENTARGUMENT =&__ VIEWSTATE = dDwxMDgyNzIxNDQ2O3Q8O2w8aTwzPjs%2BO2w8dDw7bDxpPDE%2BO2k8Mz47aTw1PjtpPDExPjs%2BO2w8dDw7bDxpPDEzPjtpPDE3Pjs%2BO2w8dDx0PHA8cDxsPERhdGFWYWx1ZUZpZWxkO0RhdGFUZXh0RmllbGQ7PjtsPFNFU1NJT05JRDtJVEVN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6X0dvVG87Pj5NHcFbPBNzNuwxs7sYLdUE2omkjw%3D%3D&_MenuCtrl%3AddlSession = 885&ddlStatus = 0&ddlSortedBy = 1&_MenuCtrl%3A_GoTo.x = 37&_MenuCtrl%3A_GoTo.y = 12

IE Header似乎有一个额外的cookie(WT_FPC = id = 83.217.99.254-2364242496.30021299:lv = 1249572414567:ss = 1249572414567;)巫婆可以通过WebTrends Cookie插件使用cookie来跟踪访问者.POST请求都返回HTTP状态代码302并重定向到返回状态200的GET请求.

有任何想法吗 ?

推荐阅读
吻过彩虹的脸_378
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有