问题描述
如果网站弹出 503,那么 webclient 只会抛出异常.
If website pops out a 503 then webclient will just throw an exception.
例如,转到 http://www.google.com/sorry/?continue=http%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Dkucing
如果我们在 Internet Explorer 中打开它,它将打开一个页面.如果我们使用 livehttpheader,它会返回 503 而不是 200.但是,它仍然显示了一些东西.
If we open it in internet explorer it'll open a page. If we use livehttpheader it returns a 503 rather than 200. However, it still show something.
现在尝试卷曲同一页面.
Now try curl the same page.
http:///www.google.com/sorry/?continue=http%3A%2F%2Fwww.google.com%2Fsearch%3Fq%3Dkucing
curl 将停止失败.那么如何让 curl 像 200 一样对待 503?
The curl will just stop failing. So how to make curl treat 503 like 200?
示例是我们尝试在 google 上搜索的内容.谷歌有时需要验证码.好吧,把它弹出来让我填写验证码.但是 webclient 只是抛出异常而不将内容分配给文件.那不好.
Samples are when we try to search something at google. Google sometimes require captcha. Well, just pop that out so I can fill the captcha. But webclient simply throw an exception without assigning the content to a file. That's not good.
如何确保 webclient 不会扔东西.
How to ensure that webclient do not throw things out.
卷曲也一样
推荐答案
对于 WebClient
,您需要处理 WebException.Response
.例如.这个 LINQPad 查询转储了我的网络服务器的未找到"错误网页提供的 HTML:
For WebClient
you need to process the WebException.Response
. E.g. this LINQPad query dumps the HTML provided by my web server's "Not Found" error web page:
Dim wc = New System.Net.WebClient
Try
Dim rd = wc.DownloadData(New Uri("http://localhost/test"))
rd.Dump
Catch Ex As System.Net.WebException
Dim rs = Ex.Response
Call (New StreamReader(rs.GetResponseStream)).ReadToEnd.Dump
End Try
这篇关于如何确保 webclient/curl 像处理 200 一样处理 503 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!