Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

  • Posting to same script using Network Load Balancing works
  • C# App can get data as long as no data is posted using the same code.

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;
    }
like image 912
Brad Wickwire Avatar asked Nov 27 '25 05:11

Brad Wickwire


1 Answers

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;
like image 167
Brad Wickwire Avatar answered Dec 08 '25 14:12

Brad Wickwire



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!