GCE - HTTP 负载平衡 returns 错误 502(网关错误) - 仅当通过 C# 发布时
GCE - HTTP Load Balancing returns Error 502 (Bad Gateway) - Only When Posting via C#
我们有一个 C# 应用程序可以获取数据并将其发布到我们的网站。在使用 Compute Engine 测试 HTTP 负载平衡时,我们遇到的唯一问题是 C# 应用程序尝试提交数据并返回 502 Bad Gateway。是否需要在 HTTP 负载平衡中设置或配置其他内容?正如我提到的,这似乎是我们遇到的唯一问题。
注意事项
- 使用网络负载平衡发布到同一脚本有效
- C#应用可以获取数据,只要没有使用相同代码发布的数据。
有效的代码
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。要解决此问题,您必须添加以下代码行之一。
System.Net.ServicePointManager.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;
我们有一个 C# 应用程序可以获取数据并将其发布到我们的网站。在使用 Compute Engine 测试 HTTP 负载平衡时,我们遇到的唯一问题是 C# 应用程序尝试提交数据并返回 502 Bad Gateway。是否需要在 HTTP 负载平衡中设置或配置其他内容?正如我提到的,这似乎是我们遇到的唯一问题。
注意事项
- 使用网络负载平衡发布到同一脚本有效
- C#应用可以获取数据,只要没有使用相同代码发布的数据。
有效的代码
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。要解决此问题,您必须添加以下代码行之一。
System.Net.ServicePointManager.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;