HttpWebRequest 服务器不可用 503 问题
我最初使用 WebRequest 和 WebResponse 来发送 Http Post 消息。 我总是得到“好的”答复。 我发布的消息是一个用 xml 中的证书签名的 XML。
组成是这样的: 发送到 https 网站的 C# 服务。 HTTPS 网站在另一个我不能说的地方。 HTTPS 本地网站仅接收我在本地发布的消息并将结果写入文件。 只是为了模拟其他网站所得到的内容。
本地网站使用自签名证书进行签名,该证书将于 2048 年到期。
该代码在本周之前一直运行良好。 我总是发帖并得到一个好的结果。 在两个网站中。 但本周测试和真正的项目实施都进行了Kaput。 在两个网站上。
在本地网站上,提示无法连接到 SSL。 这个问题是由自签名证书引起的,由于某种超出我理解的原因,它给了地狱。 感谢这里的问题,我刚刚验证了证书始终是真实的,现在它不再有问题了。
要解决此问题,只需编写以下内容:
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
在应用程序的开头。 这样它只运行一次。
剩下的问题是“远程服务器返回错误:(503) 服务器不可用。”。 我在浏览器中输入 URL,它对我来说效果很好。 在代码中,该网站没有收到任何内容,当它进入网络响应时,它给出了上述错误,
我做了一个测试应用程序,仅发送“测试 1 2 3”,但我不断收到错误。 我也发送到哈佛https网站,没有错误。
private void btnSend_Click(object sender, EventArgs e)
{
try
{
WebRequest req = WebRequest.Create(cboUrl.Text);
req.PreAuthenticate = true;
req.UseDefaultCredentials = true;
req.Method = "POST";
req.ContentType = "text/xml";
String msg = txtMsg.Text;
using (Stream s = req.GetRequestStream())
{
try
{
s.Write(
System.Text.ASCIIEncoding.ASCII.GetBytes(msg), 0, msg.Length);
}
finally
{
s.Close();
}
}
WebResponse resp = req.GetResponse();
StreamReader str = new StreamReader(resp.GetResponseStream());
txtRes.Text = str.ReadToEnd();
}
catch (WebException ex)
{
txtRes.Text = ex.Message;
}
catch (Exception ex)
{
txtRes.Text = ex.Message;
}
}
这是我根据在互联网上找到的内容构建的另一个示例:
private void button1_Click(object sender, EventArgs e)
{
try
{
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(cboUrl.Text);
myReq.Headers.Clear();
myReq.Method = "POST";
myReq.KeepAlive = false;
myReq.ProtocolVersion = HttpVersion.Version11;
myReq.ContentType = "text/xml";
myReq.Proxy = null;
myReq.Credentials = null;
myReq.ContentLength = txtMsg.Text.Length;
using (StreamWriter sendingData = new StreamWriter(myReq.GetRequestStream()))
{
sendingData.Write(txtMsg.Text);
sendingData.Flush();
sendingData.Close();
}
HttpWebResponse myResponse = (HttpWebResponse) myReq.GetResponse();
StreamReader responseStream = new StreamReader(myResponse.GetResponseStream());
txtRes.Text = responseStream.ReadToEnd();
responseStream.Close();
myResponse.Close();
}
catch(WebException ex )
{
txtRes.Text = ex.Message;
}
catch (Exception ex)
{
txtRes.Text = ex.Message;
}
}
更新
错误是我使用 httpwebrequest 调用的那个,需要一些我没有提供的 httpheaders。 之前发生的唯一一件事是我得到了“好的”答复。 他们修复了他们的代码,我修复了我的代码,现在它可以工作了。
如果发生在其他人身上,请像下面所说的那样检查代理设置,并检查另一方是否给出异常或根本不返回任何内容。
I originally used WebRequest and WebResponse to sent Http Post Messages. Always I got a response of "OK". The message I post is an XML signed with a certificate in the xml.
The composition is this:
C# service that is sending to a https website.
HTTPS Website on another place that I cant say.
HTTPS Local Website locally that is just receiving the messages I post locally and writing the results to a file. Just to simulate what the other website is getting.
Local Website is signed with a self signed certificate to expire in 2048.
This code was working fine until this week. I always posted and got an OK. In both websites. But this week the test and the real project implementation both go Kaput. On Both Websites.
On the local website it was saying unable to connect to SSL.
This problem is caused by the self signed certificate that for some reason beyond my understanding its giving hell. Thanks to the questions here I just validated the certificate to always be true and now it is not bugging anymore.
To fix this just write this:
ServicePointManager.CertificatePolicy = new AcceptAllCertificatePolicy();
In the start of your application. So that it only runs once.
The remaining problem is the "The remote server returned an error: (503) Server Unavailable.". I enter the URL in my browser and it works fine for me. In the code this website is not receiving anything and when it goes to the web response it gives me the above error
I did a test application that only sends "Testing 1 2 3" but I keep getting the error. I also sent it to a harvard https website and there was no errors.
private void btnSend_Click(object sender, EventArgs e)
{
try
{
WebRequest req = WebRequest.Create(cboUrl.Text);
req.PreAuthenticate = true;
req.UseDefaultCredentials = true;
req.Method = "POST";
req.ContentType = "text/xml";
String msg = txtMsg.Text;
using (Stream s = req.GetRequestStream())
{
try
{
s.Write(
System.Text.ASCIIEncoding.ASCII.GetBytes(msg), 0, msg.Length);
}
finally
{
s.Close();
}
}
WebResponse resp = req.GetResponse();
StreamReader str = new StreamReader(resp.GetResponseStream());
txtRes.Text = str.ReadToEnd();
}
catch (WebException ex)
{
txtRes.Text = ex.Message;
}
catch (Exception ex)
{
txtRes.Text = ex.Message;
}
}
This is another example I built from what I found in the internet:
private void button1_Click(object sender, EventArgs e)
{
try
{
HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(cboUrl.Text);
myReq.Headers.Clear();
myReq.Method = "POST";
myReq.KeepAlive = false;
myReq.ProtocolVersion = HttpVersion.Version11;
myReq.ContentType = "text/xml";
myReq.Proxy = null;
myReq.Credentials = null;
myReq.ContentLength = txtMsg.Text.Length;
using (StreamWriter sendingData = new StreamWriter(myReq.GetRequestStream()))
{
sendingData.Write(txtMsg.Text);
sendingData.Flush();
sendingData.Close();
}
HttpWebResponse myResponse = (HttpWebResponse) myReq.GetResponse();
StreamReader responseStream = new StreamReader(myResponse.GetResponseStream());
txtRes.Text = responseStream.ReadToEnd();
responseStream.Close();
myResponse.Close();
}
catch(WebException ex )
{
txtRes.Text = ex.Message;
}
catch (Exception ex)
{
txtRes.Text = ex.Message;
}
}
Update
Error was that the one I was calling with httpwebrequest, needed some httpheaders that I was not providing. Before the only thing that happened was that I got an "OK" response. They fixed their code and I fixed mine and now its working.
If it happens to someone else check like the one below said the proxy settings and also check if the other side is giving an exception or returning nothing at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我也遇到了类似的 503 错误问题,但原因是代理。 如果您有代理,只需使用:
request.Proxy = null;
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
I had similar problem with 503 error, but reason for it was proxy. If you have proxy, just use this:
request.Proxy = null;
request.Credentials = System.Net.CredentialCache.DefaultCredentials;
如果您重置 IIS,请确保您最近没有更改过密码。 很愚蠢,但它确实发生了。
If you reset IIS make sure you haven't changed your password recently. Silly but it happens.
如果您在浏览器中导航到 URL 时没有收到 503 错误,但在使用 HttpWebRequest 请求资源时确实收到该错误,我建议的第一件事是您在发出请求时指定 UserAgent 的值。
您可能还想使用 Fiddler2 或其他工具来更好地了解在请求的生命周期。 如果不了解您要发布消息的服务的详细信息,就很难提供指导。
If you are not recieving a 503 error when navigating to the URL in your browser, but do recieve it when requesting the resource when using HttpWebRequest, the first thing I would recommend is that you specify a value for the UserAgent when making the request.
You may also want to use Fiddler2 or another tool to get a better idea of what is happening during the lifetime of the request. It is hard to provide guidance without knowing more about the details of the service you are posting messages to.
我有类似的问题。 感谢Fiddler提示,我发现这是由于我的新开发环境中的代理设置引起的。 很烦人。 .NET Web 服务采用 IE 中设置的代理设置。
有关详细信息,请查看:http://support.microsoft.com/kb/307220和 http://msdn.microsoft.com/en-us/library/kd3cf2ex .aspx 。
I had a similar problem. Thanks to the Fiddler tip, I found out that it was caused by proxy settings in my new development environment. Very annoying. The .NET webservices adopt the proxy settings set in IE.
For more information, check out: http://support.microsoft.com/kb/307220 and http://msdn.microsoft.com/en-us/library/kd3cf2ex.aspx .
问题似乎是该网站已更改。 以前,如果我发送一条包含任何垃圾邮件的消息,它会返回通常的“OK”。 他们更改了网站,并且需要一些 http 标头。 昨天我一整天都有http headers,晚上才是它工作的时候。 就我而言,它期待 http 标头但没有获取它们,也许有些东西正在爆炸。
我今天测试了它,给出了标头,但没有给出 http 标头,在后一种情况下,它执行了 503。
如果我找到更多信息,我会尝试将其发布在这里。 我希望这可以为其他人节省一些时间和一些头发:)。
The problem seems to be that this website changed. Before if I sent a message with any junk it would return the usual OK. They changed the website and there are some http headers required. Yesterday I had the http headers for the whole day and at night was when it worked. In my case it was that it is expecting the http headers and not getting them and maybe something is exploding.
I tested it today giving the headers and not giving the http headers and in the latter case it did the 503.
If I find more info I will try to post it here. I hope this saves some time and some hair to other people :).