【问题标题】:Not getting expected response data after posting发布后未获得预期的响应数据
【发布时间】:2014-09-01 03:43:51
【问题描述】:

早些时候我在发布数据和获取响应时遇到问题,我终于能够发布数据,但响应没有给我正确的结果,我浏览了网络表单,然后运行了我的代码使用 Visual Studio 并使用 Fiddler 比较了网络表单,并且从我所看到的内容中正确填充了它。 然后我通过网站和视觉工作室比较了这两个结果并比较了它们,我没有得到我应该得到的结果,我不知道为什么,并且在过去的几个小时里一直在努力弄清楚我是什么做错了(之前确实发布了一个问题,并且对我想要做的事情有一些指导,所以如果你早些时候看到这样的事情,那么所有的道歉,我不得不说得更清楚)

这是我写的代码

 public static string PostMyData()
    {
        // This is where the data is going to be posted
        string url = @"http://www.cpso.on.ca/Public-Register/All-Doctors-Search.aspx";

        // This is the data that i am going to post
        string postData = "manScript_HiddenField=&" +
            "__EVENTTARGET=p%24lt%24ctl03%24pageplaceholder%24p%24lt%24ctl03%24AllDoctorsSearch%24btnSubmit&" +
            "__EVENTARGUMENT=&__LASTFOCUS=&lng=en-CA&p%24lt%24ctl00%24SearchBox%24txtWord=Site+Search&p%" + 
            "24lt%24ctl03%24pageplaceholder%24p%24lt%24ctl03%24AllDoctorsSearch%24txtLastName=Aalders&p%" +
            "24lt%24ctl03%24pageplaceholder%24p%24lt%24ctl03%24AllDoctorsSearch%24txtFirstName=&p%" +
            "24lt%24ctl03%24pageplaceholder%24p%24lt%24ctl03%24AllDoctorsSearch%24grpGender=+&p%" +
            "24lt%24ctl03%24pageplaceholder%24p%24lt%24ctl03%24AllDoctorsSearch%24ddLanguage=08&p%" +
            "24lt%24ctl03%24pageplaceholder%24p%24lt%24ctl03%24AllDoctorsSearch%24grpDocType=rdoDocTypeAll&p%" +
            "24lt%24ctl03%24pageplaceholder%24p%24lt%24ctl03%24AllDoctorsSearch%24grpStatus=rdoStatusActive&p%" +
            "24lt%24ctl03%24pageplaceholder%24p%24lt%24ctl03%24AllDoctorsSearch%24ddCity=Select --%3E&p%" +
            "24lt%24ctl03%24pageplaceholder%24p%24lt%24ctl03%24AllDoctorsSearch%24txtPostalCode=&p%" +
            "24lt%24ctl03%24pageplaceholder%24p%24lt%24ctl03%24AllDoctorsSearch%24ddHospitalCity=Select+--%3E&p%" +
            "24lt%24ctl03%24pageplaceholder%24p%24lt%24ctl03%24AllDoctorsSearch%24ddHospitalName=-1&" +
            "__VIEWSTATE=";

        // Create my request
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
        req.ContentLength = postData.Length;
        req.Referer = @"http://www.cpso.on.ca/Public-Register/All-Doctors-Search.aspx";
        req.Accept = "text/html, application/xhtml+xml, */*";
        req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";

        // Now its time to write the data that I want to post to the webpage
        using (StreamWriter reqWriter = new StreamWriter(req.GetRequestStream()))
        {
            reqWriter.Write(postData);
        }

        // Get the response/results

        string respData = string.Empty;

        using (StreamReader responseReader = new StreamReader(req.GetResponse().GetResponseStream()))
        {
            // Add response/results to string
            respData = responseReader.ReadToEnd();
        }

        return respData;

    }

我在返回的 respData 处设置了一个断点,它应该显示一条记录,但它没有显示。

这是图片,其中一张是 Fiddler 中显示的网络表单,来自实际的网络表单..

这是我通过 Visual Studio 运行它时的那个,这让我相信我正确地发布了它,因为它是一样的

【问题讨论】:

    标签: c# asp.net post get httpwebrequest


    【解决方案1】:

    看起来表单发布到/Public-Register/All-Doctors-Search.aspx,但浏览器被重定向到/Public-Register-Info-(1)/Doctor-Search-Results 以显示结果。他们似乎正在使用 ASP.NET 会话来维护两个页面之间的状态。由于会话依赖于 cookie,因此您必须在 HttpWebRequest 上启用 cookie,方法是创建一个 CookieContainer...

    // Create my request
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.CookieContainer = new CookieContainer();
    

    现在它应该可以工作了:Live Demo

    【讨论】:

    • 哇,我所缺少的只是一段代码。感谢您为我解决这个问题。另一个优点是我直到现在才知道 ASP.NET 有一个小提琴 :-)
    • 是的。这很方便。希望你没有做任何邪恶的事情。 :)
    • 我什至不知道从哪里开始制作邪恶的东西哈哈。
    • 您有什么办法可以帮我获取下一页的结果吗?我尝试了许多不同的方法,但在尝试获取第二页的响应时一直出错
    • WebForms 使用视图状态来维护页面之间的状态。从第一个响应中获取视图状态并将其与下一个响应一起发送。这段代码可以使用一些主要的重构和错误检查,但看起来像这样......dotnetfiddle.net/hmFL3q
    猜你喜欢
    • 2015-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 2020-12-17
    • 1970-01-01
    相关资源
    最近更新 更多