2014-09-01 130 views
0

早些時候,我在發佈數據和獲取響應時遇到了問題,我終於能夠獲取發佈的數據,但響應沒有給我正確的結果,我通過了然後使用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

回答

1

貌似形式員額/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

相關問題