我们有一个 C# 应用程序,可以获取数据并将其发布到我们的网站。在使用 Compute Engine 测试 HTTP 负载平衡时,我们遇到的唯一问题是当 C# 应用程序尝试提交数据时返回 502 Bad Gateway。 HTTP 负载平衡中是否需要设置或配置其他内容?就像我提到的,这似乎是我们遇到的唯一问题。
注意事项
- 使用网络负载平衡发布到同一脚本有效
- 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。要解决此问题,您必须添加以下代码行之一。
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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)