为什么用httpwebrequest方式获取ajax的json返回值,结果返回html
以下为程序代码,还请各位大神赐教,为什么我获取到返回值是html页面
Private Function RequestPost(ByVal url As String) As String
Dim returnstring As String = ""
Dim contentType As String = "application/x-www-form-urlencoded"
Dim accept As String = "application/json, text/javascript, */*; q=0.01"
Dim userAgent As String = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:33.0) Gecko/20100101 Firefox/33.0"
Dim referer As String = ""
Dim params As String = "orderNo=&source=JINGD"
Dim httpWebRequest As HttpWebRequest = Nothing
Try
httpWebRequest = WebRequest.Create(url)
httpWebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"
httpWebRequest.Referer = referer
httpWebRequest.Accept = accept
httpWebRequest.UserAgent = userAgent
httpWebRequest.Method = "POST"
httpWebRequest.KeepAlive = True
httpWebRequest.ServicePoint.ConnectionLimit = 100
httpWebRequest.Headers.Set("x-requested-with", "XMLHttpRequest")
Dim bs As Byte() = Encoding.ASCII.GetBytes(params)
httpWebRequest.ContentLength = bs.Length
Using reqStream As Stream = httpWebRequest.GetRequestStream()
reqStream.Write(bs, 0, bs.Length)
reqStream.Close()
End Using
Dim response As HttpWebResponse = CType(httpWebRequest.GetResponse(), HttpWebResponse)
Using reader As StreamReader = New StreamReader(response.GetResponseStream())
returnstring = reader.ReadToEnd()
End Using
httpWebRequest.Abort()
response.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return returnstring
End Function
第一次 提问题,可能没说明白,还请见谅,账号没分了,sorry
------解决思路----------------------
httpwebrequest只是如实的返回服务器传回的数据流,并没有限定json格式,比如对面返回404,你拿到就可能是html,因为服务器如果不对404封装为json格式,你拿到也不会是json
------解决思路----------------------
从你拿到HTML看,貌似你的请求在服务器解析后没有返回你json而是直接反回了一个页面,但你浏览器却能拿到JSON,那可能是你提交的请求数据上有区别,致使服务器得出不同的返回结果,你可以用工具抓包看看浏览器请求和.net请求的区别