Brad Wickwire
Brad Wickwire

Reputation: 1153

GCE - HTTP Load Balancing returns Error 502 (Bad Gateway) - Only When Posting via C#

We have a C# application that gets and posts data to our website. While testing out HTTP Load Balancing with Compute Engine, the only problem we have is when the C# App tries to submit data and a 502 Bad Gateway is returned. Is there something additional that has to be set or configured in the HTTP Load Balancing? Like I mentioned, this appears to be the only issue we are having.

Things to note

Code that works

SendRequest("http://beta.stubwire.com/", "");

Code that doesnt work

SendRequest("http://beta.stubwire.com/", "this is a test");

Function that is called

        private static string SendRequest(string url, string postdata)
    {
        if (String.IsNullOrEmpty(postdata))
            return null;
        HttpWebRequest rqst = (HttpWebRequest)HttpWebRequest.Create(url);
        // No proxy details are required in the code.
        rqst.Proxy = GlobalProxySelection.GetEmptyWebProxy();
        rqst.Method = "POST";
        rqst.ContentType = "application/x-www-form-urlencoded";
        // In order to solve the problem with the proxy not recognising the user
        // agent, a default value is provided here.
        rqst.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
        byte[] byteData = Encoding.UTF8.GetBytes(postdata);
        rqst.ContentLength = byteData.Length;

        using (Stream postStream = rqst.GetRequestStream())
        {
            postStream.Write(byteData, 0, byteData.Length);
            postStream.Close();
        }
        StreamReader rsps = new StreamReader(rqst.GetResponse().GetResponseStream());
        string strRsps = rsps.ReadToEnd();
        return strRsps;
    }

Upvotes: 3

Views: 803

Answers (1)

Brad Wickwire
Brad Wickwire

Reputation: 1153

Google HTTP Load Balancing does not support the Expect 100 Continue. To fix this you have to add one of the below lines of code.

System.Net.ServicePointManager.Expect100Continue = false;

rqst.ServicePoint.Expect100Continue = false;

Example

        HttpWebRequest rqst = (HttpWebRequest)HttpWebRequest.Create(url);
        rqst.Proxy = GlobalProxySelection.GetEmptyWebProxy();
        rqst.Method = "POST";
        rqst.ServicePoint.Expect100Continue = false;
        rqst.ContentType = "application/x-www-form-urlencoded";
        rqst.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)";
        byte[] byteData = Encoding.UTF8.GetBytes(postdata);
        rqst.ContentLength = byteData.Length;

Upvotes: 3

Related Questions