发布后没有收到预期的响应数据

发布后没有收到预期的响应数据

问题描述:

早些时候,我在发布数据和获取响应时遇到了问题,我终于能够获取发布的数据,但响应没有给我正确的结果,我通过了然后使用Visual Studio运行我的代码,并使用Fiddler比较webforms,并从我所能看到的内容中正确填充它。 然后,我通过网站和通过视觉工作室比较了两个结果,并对它们进行了比较,我没有得到我应该得到的结果,我不知道为什么,并且在最近几个小时试图弄清楚我我做错了(之前发布了一个问题,并有一些指导与我期待做的事情,所以如果你看到这样的事情早些时候,所有的道歉,我不得不使它更清楚)发布后没有收到预期的响应数据

这里是代码我写了

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处设置了一个断点,它应该显示一条记录,但它不显示任何记录。

下面是图片,一个是通过实际的网页表单将在提琴手所示的网络表单..

From Site

这里是一个当我运行它通过视觉工作室,这导致我相信,我张贴正确的,因为它是同

From VS

貌似形式员额/Public-Register/All-Doctors-Search.aspx但浏览器重定向到/Public-Register-Info-(1)/Doctor-Search-Results显示结果。他们似乎在使用ASP.NET会话来维护两个页面之间的状态。由于会话靠饼干,你必须创建一个CookieContainer以启用HttpWebRequest饼干...

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

现在它应该工作:Live Demo的代码

+0

哇,我缺的是一块。感谢您为我清理。另外一个优点是,直到现在我还不知道ASP.NET有一个小提琴:-) – Chris 2014-09-01 04:22:15

+0

是的。这非常方便。希望你没有做任何邪恶的事情。 :) – 2014-09-01 04:25:25

+0

我甚至不知道从哪里开始做出一些邪恶的事情。 – Chris 2014-09-01 04:37:28