- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 HttpWebRequest,但在执行 GetResponse() 时出现错误。
我使用这段代码:
private void button1_Click(object sender, EventArgs e)
{
Uri myUri = new Uri("http://www.google.com/sorry/?continue=http://www.google.com/search%3Fq%3Dyamaha");
// Create a 'HttpWebRequest' object for the specified url.
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
// Set the user agent as if we were a web browser
myHttpWebRequest.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4";
HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
var stream = myHttpWebResponse.GetResponseStream();
var reader = new StreamReader(stream);
var html = reader.ReadToEnd();
// Release resources of response object.
myHttpWebResponse.Close();
textBox1.Text = html;
}
最佳答案
服务器确实返回了 503 HTTP 状态代码。但是,它还会返回一个响应正文以及 503 错误条件(如果您打开该 URL,您会在浏览器中看到的内容)。
您可以访问异常的 Response
属性中的响应(如果有 503 响应,引发的异常是 WebException
,它有一个 响应
属性)。你需要捕获这个异常并妥善处理它
具体来说,您的代码可能如下所示:
string html;
try
{
var myUri = new Uri("http://www.google.com/sorry/?continue=http://www.google.com/search%3Fq%3Dyamaha");
// Create a 'HttpWebRequest' object for the specified url.
var myHttpWebRequest = (HttpWebRequest)WebRequest.Create(myUri);
// Set the user agent as if we were a web browser
myHttpWebRequest.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4";
var myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
var stream = myHttpWebResponse.GetResponseStream();
var reader = new StreamReader(stream);
html = reader.ReadToEnd();
// Release resources of response object.
myHttpWebResponse.Close();
}
catch (WebException ex)
{
using(var sr = new StreamReader(ex.Response.GetResponseStream()))
html = sr.ReadToEnd();
}
textBox1.Text = html;
关于c# - HttpWeb 请求错误 : 503 server unavailable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10107305/
我正在尝试使用应用内结算: mIabHelper = new IabHelper(this, BILLING_KEY); mIabHelper.startSetup(new IabHe
我找不到 contentsOfURL 的任何替代方法来解决此问题。 这是我的 Swift 代码: import Foundation import MapKit class MapHelper{ fu
我正在尝试通过运行客户端-服务器套接字程序来测试本地个人计算机和虚拟机之间的连接。 这就是我正在做的事情: 1) 我正在 vm 播放器上运行 Kubuntu 10.10。2)安装java 63)通过k
当我尝试对在我的机器上(在同一网络上)运行的 python RPC 服务器进行 RPC 调用时,我一整天都在为 Android 上 gRPC 的以下问题而苦苦挣扎。 我的 android gRPC a
我已按照教程将 .net core Web 应用程序构建到 Docker 镜像中,发布到 azure 容器注册表,然后设置了 VSTS 发布模板以将容器部署到应用服务。 这一切似乎都有效,我可以在容器
我在我的 flutter 应用程序中使用 Firebase Firestore。 debug 模式下一切正常。但是,在 release 模式下,我收到以下错误。我看到了一个类似的问题,但它并没有解决我
每次我尝试发布网站时,它都运行良好,但是当网站打开时,它给我一个, “HTTP 错误 503。服务不可用。” 我在互联网上到处都在谈论它是我的应用程序池,但它似乎运行良好。我认为它可能是我的 web.
我有一个写到SQL Server数据库的log4net ado附加程序。我喜欢它,我认为它很整洁。在将其发送到生产环境之前,我想知道如果数据库关闭,行为将会如何。 我不希望应用程序停止,因为日志数据库
我使用YouTube分享按钮中的youtube iframe嵌入代码,得到了此代码 我已在网页中运行它,并且缩略图也加载了微调器加载,然后出现错误“视频不可用” 屏幕截图: 最佳答案 如果您直接从磁
我在HTML 5媒体播放器播放列表中有YouTube视频的汇总列表(从mySql数据库查询)。但是随着时间的流逝,由于版权或判断问题,YouTube将禁用某些视频,但是这些链接仍在我的列表中。任何人都
我有一个使用 ARM 模板创建的 python 函数应用程序。当我尝试使用 azure Devops 管道部署函数时,我遇到以下错误, “无法将Web包部署到应用服务。服务暂时不可用代码503” 最佳
有没有办法定义自定义 HTML 页面或在站点离线时重定向到另一个站点,而不是使用通用的 Site unavailable page ? 我发现网站离线有 2 个原因: 管理员在 Azure 门户中停止
我正在使用灯泡。我所做的是我有一个 php 页面,其中包含向 ajax 中的所有按钮发送消息。当我单击该按钮时,它会调用后端功能并向 10 个用户发送消息,然后显示每 10 个用户发送一次的结果。它工
我的应用程序启动后,我在日志中看到这条消息: Started Application in 22.609 seconds (JVM running for 24.342) 但是当我在浏览器中访问该应用
我正在使用 knockout ,我想比较某个值是否不可用。 JavaScript/knockout 有没有办法可以做到这一点? Your defined 我已经尝试过未定义,但这不起
我正在使用 IAR(版本:6.60.1.5104)来运行我的 C 项目。 我看到了一个奇怪的行为。 在调试时,我正在获取本地定义为 zero 的变量值 例如。 int a = 0 但对于分配非零值的变
最近我将我的 Nginx/Gunicorn/Django 网站“mysite”转换为 SSL,并且 SSL 连接工作正常。对于该站点以前的非 SSL 版本,我在我的 Nginx 配置文件中创建了一些指
如果我在 Python 解释器中创建了一个包含大量进程的池,它显然会出错,但是在这样做之前似乎并没有清理 fork 进程,因此留下了环境脏,系统的其余部分无法 fork 进程。 >>> from mu
我有一些带有 class='class_name' 的 div,并且还声明了 var A = document.getElementsByClassName('class_name'); consol
我正在将我的应用程序从 Swift“升级”到 Swift 2 并遇到以下错误:'deviceInputWithDevice' 不可用:使用对象构造 'AVCaptureDeviceInput(devi
我是一名优秀的程序员,十分优秀!