GCE-HTTP负载平衡返回错误502(错误的网关)-仅当通过C#发布时 [英] GCE - HTTP Load Balancing returns Error 502 (Bad Gateway) - Only When Posting via C#

查看:60
本文介绍了GCE-HTTP负载平衡返回错误502(错误的网关)-仅当通过C#发布时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个C#应用程序,可以获取数据并将其发布到我们的网站.在使用Compute Engine测试HTTP负载平衡时,我们唯一的问题是C#App尝试提交数据并返回502 Bad Gateway时.是否需要在HTTP负载平衡中设置或配置其他功能?就像我提到的那样,这似乎是我们唯一遇到的问题.

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.

注意事项

  • 使用网络负载平衡功能发布到相同的脚本中
  • 只要没有使用相同代码发布的数据,C#App就可以获取数据.

有效的代码

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

无效的代码

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

被调用的功能

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

推荐答案

Google HTTP负载平衡不支持Expect 100 Continue.要解决此问题,您必须添加以下代码行之一.

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;

System.Net.ServicePointManager.Expect100Continue = false;

rqst.ServicePoint.Expect100Continue = false;

rqst.ServicePoint.Expect100Continue = false;

示例

        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;

这篇关于GCE-HTTP负载平衡返回错误502(错误的网关)-仅当通过C#发布时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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