300 分求助 ajax post HttpWebRequest

songhuan 2008-12-05 10:41:46
POST /ajax/NRT.WebSite.PropertySearch.Business.SearchUIController,NRT.Website.PropertySearch.ashx?_method=GetResultsHTML&_session=rw HTTP/1.1
Host: www.coldwellbankermove.com
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.4) Gecko/2008102920 Firefox/3.0.4
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://www.coldwellbankermove.com/property/PropertyResults.aspx
Content-Length: 128
Content-Type: application/xml; charset=UTF-8
Cookie: StateID=26; WT_FPC=id=2789c3ba7af6546c7401226939806400:lv=1228328694364:ss=1228328097037; ARPT=WINPMOS172.16.25.192CKMWY; ASP.NET_SessionId=atfafh55us4sdhaebvvgimmt; OAS_SC1=1228379093650
Pragma: no-cache
Cache-Control: no-cache
searchID=35677
pageNumber=1
maxListingsPerPage=10
sortColumn=0
sourceID=-1
displayMode=0
bIsConsumerSearch=false
dtSince=



以上是本人使用Live Http Headers抓取的post数据,小弟有一个地方不明白,在Cache-Control: no-cache之后,post的数据是这样的,
searchID=35677
pageNumber=1
maxListingsPerPage=10
sortColumn=0
sourceID=-1
displayMode=0
bIsConsumerSearch=false
dtSince=
可是一般都是searchID=35677&pageNumber=1&maxListingsPerPage=10 这样的 name=value&name=value
请教我要使用HttpWebRequest post的时候,post的数据体怎么写?
本人已经试过在每一行post数据之后加\r\n,可是光加这个是不行的。请指教.


...全文
189 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
peacock 2008-12-05
  • 打赏
  • 举报
回复
name=value&name=value
这种形式是GET方式,而你抓取的数据采用的是POST方法,你可以对HttpWebRequest进行Method设置,比如


HttpWebRequest vReq = (HttpWebRequest) HttpWebRequest.Create("http://www.coldwellbankermove.com/property/PropertyResults.aspx");
vReq.Method = "POST"; //POST方法
//vReq.Method = "GET"; //GET方法
using (WebResponse vWR = vReq.GetResponse())
{
//在这里对接收到的页面内容进行处理
}


songhuan 2008-12-05
  • 打赏
  • 举报
回复
searchID=35677\r\npageNumber=12\r\nmaxListingsPerPage=10\r\nsortColumn=0\r\nsourceID=-1\r\ndisplayMode=0\r\nbIsConsumerSearch=False\r\ndtSince=

字符串这样写,供碰到同样问题的兄弟参考
tianshangfei 2008-12-05
  • 打赏
  • 举报
回复
学习~
songhuan 2008-12-05
  • 打赏
  • 举报
回复
感谢peacock,问题已自己解决
post的时候需要特殊处理
searchID=35677
pageNumber=1
maxListingsPerPage=10
sortColumn=0
sourceID=-1
displayMode=0
bIsConsumerSearch=false
dtSince=

把这几个参数分开转化为字节数组后,合并自己数组并在中间加上13 10



If (PostData <> String.Empty) Then
request.Method = "POST"
request.ContentType = "application/x-www-form-urlencoded"
Dim byteArray As Byte()
Dim NewLineIndex As Integer = PostData.IndexOf("\r\n")
If (NewLineIndex = -1) Then
byteArray = Encoding.UTF8.GetBytes(PostData)
Else
Dim NewPost As New Hashtable
While NewLineIndex <> -1
byteArray = Encoding.UTF8.GetBytes(PostData.Substring(0, NewLineIndex))
For i As Integer = 0 To byteArray.Length - 1
NewPost(NewPost.Count) = byteArray(i)
Next
PostData = PostData.Substring(NewLineIndex + 4, PostData.Length - NewLineIndex - 4)
NewPost(NewPost.Count) = 13
NewPost(NewPost.Count) = 10
NewLineIndex = PostData.IndexOf("\r\n")
End While
byteArray = Encoding.UTF8.GetBytes(PostData)
For i As Integer = 0 To byteArray.Length - 1
NewPost(NewPost.Count) = byteArray(i)
Next
Dim byteArrayTem(NewPost.Count - 1) As Byte

For i As Integer = 0 To byteArrayTem.Length - 1
byteArrayTem(i) = NewPost(i)
Next
byteArray = byteArrayTem
End If
request.ContentLength = byteArray.Length
'post
Dim newStream As Stream = request.GetRequestStream()
newStream.Write(byteArray, 0, byteArray.Length)
newStream.Flush()
newStream.Close()

End If
songhuan 2008-12-05
  • 打赏
  • 举报
回复
sorry,是这个地址

http://www.coldwellbankermove.com/ajax/NRT.WebSite.PropertySearch.Business.SearchUIController,NRT.Website.PropertySearch.ashx?_method=GetResultsHTML&_session=rw
songhuan 2008-12-05
  • 打赏
  • 举报
回复
感谢楼上回复,本人在上边已有说明,此页面的post数据体跟普通的post不同,所以不能采用 name=value&name=value,这样得到的结果是错误的

再者,需要post的页面是http://www.coldwellbankermove.com//ajax/NRT.Website.MySite.Business.MySiteProvider,NRT.Website.MySite.ashx?_method=SetFormsAuthCookie&_session=no
peacock 2008-12-05
  • 打赏
  • 举报
回复

string postData = "searchID=35677";
postData += "&pageNumber=1";
postData += "&maxListingsPerPage=10";
postData += "&sortColumn=0";
postData += "&sourceID=-1";
postData += "&displayMode=0";
postData += "&bIsConsumerSearch=false";
postData += "&dtSince=";
byte[] data = encoding.GetBytes(postData);
HttpWebRequest myRequest =
(HttpWebRequest)WebRequest.Create("http://www.coldwellbankermove.com/property/PropertyResults.aspx");
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
//Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close();
songhuan 2008-12-05
  • 打赏
  • 举报
回复
回1楼,我的问题不是采取post还是get的问题,而是post的数据体怎么写的问题,感谢回复.

52,797

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 Ajax
社区管理员
  • Ajax
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧