HttpWebRequest服务器不可用503问题 [英] HttpWebRequest Server Unavailable 503 probleme

查看:871
本文介绍了HttpWebRequest服务器不可用503问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最初使用WebRequest和WebResponse发送Http Post Messages。我总是收到好的回复。我发布的消息是使用xml中的证书签名的XML。

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.

组成如下:
发送到https网站的C#服务。
HTTPS网站在另一个地方,我不能说。
本地HTTPS本地网站,它只接收我在本地发布的消息并将结果写入文件。只是为了模拟其他网站的内容。

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.

本地网站签署的自签名证书将于2048年到期。

Local Website is signed with a self signed certificate to expire in 2048.

此代码在本周工作正常。我总是张贴并且好了。在这两个网站上。但本周测试和真正的项目实施都是Kaput。在两个网站上。

在本地网站上,它说无法连接到SSL。
这个问题是由自签名证书造成的,由于某些原因我无法理解它的地狱。感谢这里的问题,我只是验证了证书永远是真的,现在它不再是烦恼了。

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.

剩下的问题是远程服务器返回错误:(503)服务器不可用。。我在浏览器中输入了URL,它对我来说很好。在代码中,这个网站没有收到任何东西,当它进入网络响应时,它给了我上面的错误

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

我做了一个只发送测试1 2的测试应用程序3但我不断收到错误。我还将它发送到哈佛https网站并且没有错误。

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;
        }


    }

更新

错误是我用httpwebrequest调用的那个,需要一些我没有提供的httpheader。在发生的唯一事情之前,我得到了一个OK响应。他们修复了我们的代码,我修复了我的代码,现在它正在运

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.

推荐答案

如果您在浏览器中导航到URL时收到503错误,但在使用HttpWebRequest时请求资源时接收它,我建议的第一件事是你在发出请求时为 UserAgent 指定一个值。

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.

您可能还想使用 Fiddler2 或其他工具来获得更好的效果了解请求生命周期中发生的事情。如果不了解更多关于您要发送消息的服务的详细信息,很难提供指导。

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.

这篇关于HttpWebRequest服务器不可用503问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆