views:

4055

answers:

3

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: CSharp service that is sending to a https website. HTTPS Website on another place that I cant say. HTTPS Local Website locally that is just recieving 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 recieving 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.

+2  A: 

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.

Oppositional
A: 

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 :).

ThorDivDev
A: 

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 .

Pascal Lindelauf