以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  我想问下如何判断HttpClient超时?  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=99077)

--  作者:chen3728060
--  发布时间:2017/4/12 12:18:00
--  我想问下如何判断HttpClient超时?

Dim hc As New HttpClient("http://search.anccnet.com/searchResult2.aspx?keyword=4891599366808")
hc.Resp
Dim str As String = hc.GetData()

 

这个网址就算电脑查询,都是很卡,估计是这个网站的问题,起码要20秒,我不想客户手机端等这么久,我设置 hc.TimeOut=5

 

问题是?我想问怎么判断HttpClient超时?

 

我用 if str <>"" then  它还是报连接超时的错误

 

--------------------------------

 

还有我想问,这个网址,我用for 循环去高频请求,请求到大概15次以后,会收到  403 服务器拒绝请求,这个是不是网站发现我的ip高频请求,所以拒绝了我?有办法可以每次随机延时0.几秒这样去查询吗?

 

那个403错误提示,这么这么捕获到?不然每次都弹出提示,不点确定,服务器端就用不了

[此贴子已经被作者于2017/4/12 12:59:43编辑过]

--  作者:chen3728060
--  发布时间:2017/4/12 14:37:00
--  

顶一下


--  作者:有点色
--  发布时间:2017/4/12 14:49:00
--  

 过快的请求,就会报403的错误。

 

 你写的httpClient代码,是运行在服务器上的,而httpRequest是单线程的,自然执行效率会低。

 

 如果是手机端的应用,你可以直接写js去读取网页的数据,得到以后,再用js分析,这样就不需要经过服务器。


--  作者:有点色
--  发布时间:2017/4/12 15:08:00
--  
测试了一下用js,实现不了,涉及到跨域的问题。你还是需要用原来的思路。
--  作者:chen3728060
--  发布时间:2017/4/12 15:54:00
--  

那怎么判断HttpClient是否超时?超时我就返回一个错误的htm给客户,而不是让客户一直等


--  作者:有点色
--  发布时间:2017/4/12 16:06:00
--  

 用httpClient麻烦,当出错的时候,会弹出错误。还是用原来的

 

try
    Dim req = System.Net.WebRequest.Create("http://search.anccnet.com/searchResult2.aspx?keyword=4891599366808")
    req.Method = "get"
    req.timeout = 2000
    Dim pos = req.GetResponse
    Dim stm As System.IO.Stream = pos.GetResponseStream()
    Dim reader As New System.IO.StreamReader(stm)
    Dim str As String = reader.ReadToEnd
   
    pos.Close
    stm.Close
    reader.close
    msgbox(str)
catch ex As exception
    msgbox("超时")
End try


--  作者:chen3728060
--  发布时间:2017/4/12 18:11:00
--  回复:(有点色)?用httpClient麻烦,当出错的时...

这个传统方法,是怎么设置 编码方式为 gb2312,读出来的中文部分是乱码

 

try
    Dim req = System.Net.WebRequest.Create("http://search.anccnet.com/searchResult2.aspx?keyword=6907992100272")
    req.Method = "get"   
    req.timeout = 5000
    Dim pos = req.GetResponse
    Dim stm As System.IO.Stream = pos.GetResponseStream()
    Dim reader As New System.IO.StreamReader(stm)
    Dim str As String = reader.ReadToEnd
   
    pos.Close
    stm.Close
    reader.close
    msgbox(str)
catch ex As exception
    msgbox("超时")
End try

[此贴子已经被作者于2017/4/12 18:11:16编辑过]

--  作者:有点色
--  发布时间:2017/4/12 19:58:00
--  

try
    Dim req = System.Net.WebRequest.Create("http://search.anccnet.com/searchResult2.aspx?keyword=6907992100272")
    req.Method = "get"
    req.timeout = 5000
    Dim pos = req.GetResponse
    Dim stm As System.IO.Stream = pos.GetResponseStream()
    Dim reader As New System.IO.StreamReader(stm,Encoding.GetEncoding("GB2312"))
    Dim str As String = reader.ReadToEnd
   
    pos.Close
    stm.Close
    reader.close
    msgbox(str)
catch ex As exception
    msgbox("超时")
End try