Search code examples
sslhttpshttpwebrequestwebrequest

HttpWebRequest Server Unavailable 503 probleme


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.


Solution

  • 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.