【发布时间】: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