发布后未获得预期的响应数据

时间:2014-09-01 03:43:51

标签: c# asp.net post get httpwebrequest

之前我在发布数据和获得响应方面遇到了问题,我终于能够发布数据了,但是响应没有给我正确的结果,我浏览了webform,然后运行了我的代码使用视觉工作室并使用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中显示的网页表单,通过实际的网络表单。

From Site

这是我通过视觉工作室运行的时候,这让我相信我发布了它是因为它是相同的

From VS

1 个答案:

答案 0 :(得分:1)

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

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

现在应该有效:Live Demo